Skip to content

Commit

Permalink
feat: Added mathematical operation type and refined parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
NotAJunkie2 committed Jan 10, 2024
1 parent 7b73f8c commit 9ff710d
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 18 deletions.
1 change: 1 addition & 0 deletions Testfile/test_call/call1.ccs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
test() => (
let x = 5;
test();
);

test();
2 changes: 1 addition & 1 deletion Testfile/test_call/call2.ccs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ let a = 5;
let b = 10;
let c = 3;

test(a b c) => (
test(b, b, c) => (
let d = (a + b + c);
);

Expand Down
5 changes: 1 addition & 4 deletions Testfile/test_define/define3.ccs
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
let x = (a b) => (
if (a == b) then:
show("a equals b");
);
letx=(a b)=>(if(a==b)then:show("a equals b"););
2 changes: 0 additions & 2 deletions Testfile/test_if/if1.ccs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
let a = 1;
let b = 1;

(a == b)

if (a == b) then:
(
let x = 10;
Expand Down
1 change: 1 addition & 0 deletions Testfile/test_math_op/math_op1.ccs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let x = 5 + (10 - 12);
12 changes: 3 additions & 9 deletions Testfile/test_named_call/named_call3.ccs
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
let a = 5;
let b = 10;
let c = 2;

test (a b c) => (
let x = (a - b + c);
);

test(a b c);
fact(nbr) => {
(nbr == 1) ? 1 : (nbr * fact((nbr - 1));)
};
17 changes: 15 additions & 2 deletions src/glados-main/CCSAstParser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ parseBody = parseMany parseWhiteSpace *> parseChar '{'
*> parseMany parseCCSAst >>= \args -> parseMany parseWhiteSpace <* parseChar '}'
Data.Functor.$> AstList args

parseArgList :: Parser Ast
parseArgList = parseMany parseWhiteSpace *> parseChar '('
*> parseMany (parseCCSAst <* parseSome (parseChar ',')) >>= \args -> parseMany parseWhiteSpace <* parseChar ')'
Data.Functor.$> AstList args

parseIf :: Parser Ast
parseIf = parseOr (parseMany parseWhiteSpace *> parseString "if" *> parseAstList >>=
\cond -> parseMany parseWhiteSpace *> parseString "then:" *> parseCCSAst >>= \thenExpr ->
Expand All @@ -65,7 +70,7 @@ parseIf = parseOr (parseMany parseWhiteSpace *> parseString "if" *> parseAstList
parseMany parseWhiteSpace Data.Functor.$> If cond thenExpr elseExpr)

parseLambda :: Parser Ast
parseLambda = parseAstList >>= \params ->
parseLambda = parseArgList >>= \params ->
parseMany parseWhiteSpace *> parseString "=>" *>
parseOr parseAstList parseBody
>>= \body -> parseMany parseWhiteSpace
Expand All @@ -78,12 +83,20 @@ parseNamedFunc = parseAstSymbol >>= \name ->

parseCall :: Parser Ast
parseCall = parseAstSymbol >>= \name ->
parseAstList >>= \args -> parseSeparator
parseArgList >>= \args -> parseSeparator
Data.Functor.$> AstCall name args

parseMathOperation :: Parser Ast
parseMathOperation = parseMany parseWhiteSpace
*> parseOr parseAstList (parseOr parseAstSymbol parseAstInt) >>= \arg1 -> parseMany parseWhiteSpace
*> parseOperator >>= \op -> parseMany parseWhiteSpace
*> parseOr parseAstList (parseOr parseAstSymbol parseAstInt) >>= \arg2 -> parseMany parseWhiteSpace
Data.Functor.$> AstMathOp arg1 (Operator op) arg2

parseCCSAst :: Parser Ast
parseCCSAst = parseAstLogicOperator
<|> parseAstOperator
<|> parseMathOperation
<|> parseIf
<|> parseDefine
<|> parseAssign
Expand Down
1 change: 1 addition & 0 deletions src/glados-main/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ data Ast = AstInt Int
| AstSymbol String
| AstString String
| AstList [Ast]
| AstMathOp Ast Ast Ast
| Assign Ast Ast
| Define Ast Ast
| If Ast Ast Ast
Expand Down

0 comments on commit 9ff710d

Please sign in to comment.