Skip to content

Commit da34dfc

Browse files
committed
[feat:root] initial Sugar
1 parent 1b7378d commit da34dfc

File tree

6 files changed

+46
-5
lines changed

6 files changed

+46
-5
lines changed

axolotl.cabal

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ executable axl
3838
, Transpiler.Transpiler
3939
, Transpiler.Util
4040
, Transpiler.Backends.JS.JS
41+
-- Desugarer
42+
, Sugar.Sugar
4143
-- Miscellaneous
4244
, Paths_axolotl
4345

example.axl

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
(print "this is my programming language, axolotl")
55

66
(defun get-given-count [(argument-for-no-reason: int)] {
7-
(+i argument-for-no-reason 1)
7+
(+ argument-for-no-reason 1)
88
})
99

1010
(def text1 "If I have ")
@@ -13,7 +13,7 @@
1313
(defun arbitrary-function [(initial: int)] {
1414
(print "expressions are written in braces")
1515
(print "and the last value is returned!")
16-
(print (str text1 (+i initial 2) " apples and I give " (get-given-count 2) text2 (/f (+i initial 2) (get-given-count 4)) " apples!"))
16+
(print (str text1 (+ initial 2) " apples and I give " (get-given-count 2) text2 (/ (+ initial 2) (get-given-count 4)) " apples!"))
1717
420
1818
})
1919

hie.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,8 @@ cradle:
6060
- path: "src/Transpiler/Backends/JS/JS.hs"
6161
component: "axolotl:exe:axl"
6262

63+
- path: "src/Sugar/Sugar.hs"
64+
component: "axolotl:exe:axl"
65+
6366
- path: "src/Paths_axolotl.hs"
6467
component: "axolotl:exe:axl"

src/Analyser/Analyser.hs

-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import Data.Either.Combinators (fromLeft', fromRight, fromRight', isLeft, maybeT
2828
import qualified Data.HashTable.IO as H
2929
import Data.Maybe (fromJust, fromMaybe, isJust)
3030
import Data.Text as T (Text, empty, pack, toLower, unpack)
31-
import Debug.Trace (trace)
3231
import Parser.Ast
3332
( Expr (ArbitraryBlock, Array, Conditional, FunctionCall, FunctionDef, Nil, Root, VariableDef),
3433
VDataType (Bool, Function, Inferred, NilType),

src/Parser/Parser.hs

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import Parser.Combinators
1919
squares,
2020
strLit,
2121
)
22+
import Sugar.Sugar (desugarExpr)
2223
import Text.Megaparsec (MonadParsec (observing, try), many, single, (<|>))
2324
import Text.Megaparsec.Char (numberChar)
2425

@@ -100,7 +101,7 @@ arraySupportedExpr =
100101

101102
expr :: Parser Expr
102103
expr =
103-
try floatLiteral
104+
desugarExpr <$> try floatLiteral
104105
<|> intLiteral
105106
<|> charLiteral
106107
<|> strLiteral
@@ -117,7 +118,7 @@ expr =
117118
<|> unary
118119

119120
exprs :: Parser [Expr]
120-
exprs = many expr
121+
exprs = map desugarExpr <$> many expr
121122

122123
root :: Parser Expr
123124
root = exprs >>= \x -> pure $ Root x

src/Sugar/Sugar.hs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module Sugar.Sugar where
2+
3+
import Analyser.Util (rFoldl)
4+
import qualified Data.Text as T
5+
import Parser.Ast (Expr (FloatLiteral, FunctionCall))
6+
7+
mFilter :: [a] -> (a -> Bool) -> [a]
8+
mFilter a b = filter b a
9+
10+
doesHaveFloat :: [Expr] -> Bool
11+
doesHaveFloat exprs =
12+
not $
13+
null
14+
( mFilter
15+
exprs
16+
( \a -> do
17+
case a of
18+
FloatLiteral _ -> True
19+
_ -> False
20+
)
21+
)
22+
23+
desugarExpr :: Expr -> Expr
24+
desugarExpr e@(FunctionCall name args) = do
25+
let mkFc =
26+
if doesHaveFloat args
27+
then FunctionCall (name <> "f") args
28+
else FunctionCall (name <> "i") args
29+
30+
case name of
31+
"+" -> mkFc
32+
"-" -> mkFc
33+
"*" -> mkFc
34+
"/" -> FunctionCall "/f" args
35+
_ -> e
36+
desugarExpr x = x

0 commit comments

Comments
 (0)