Skip to content

Commit

Permalink
add fizz-buzz
Browse files Browse the repository at this point in the history
  • Loading branch information
antonkesy committed Mar 15, 2024
1 parent 7992362 commit 0a61104
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 22 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ void main() {
}
```

## Limitations

- left side of operation has to be atomic

## Usage

```bash
Expand Down
12 changes: 12 additions & 0 deletions examples/fizzbuzz.mmm
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
int i = 1;
while i <= 100 {
println(str(i) + ": " + fizzbuzz(i));
i = i + 1;
}

str fizzbuzz(int i) {
if 0 == (i % 15) { return "FizzBuzz"; }
if 0 == (i % 3) { return "Fizz"; }
if 0 == (i % 5) { return "Buzz"; }
return str(i);
}
11 changes: 11 additions & 0 deletions src/Interpreter/Operation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,27 @@ import AST
import Interpreter.ProgramState

interpretOperation :: Operator -> Value -> Value -> Value
-- Bool
interpretOperation Lt (BoolValue left) (BoolValue right) = BoolValue $ left < right
interpretOperation Gt (BoolValue left) (BoolValue right) = BoolValue $ left > right
interpretOperation Le (BoolValue left) (BoolValue right) = BoolValue $ left <= right
interpretOperation Ge (BoolValue left) (BoolValue right) = BoolValue $ left >= right
interpretOperation Eq (BoolValue left) (BoolValue right) = BoolValue $ left == right
interpretOperation And (BoolValue left) (BoolValue right) = BoolValue $ left && right
interpretOperation Or (BoolValue left) (BoolValue right) = BoolValue $ left || right
interpretOperation Neq (BoolValue left) (BoolValue right) = BoolValue $ left /= right
-- Int
interpretOperation Plus (IntValue left) (IntValue right) = IntValue $ left + right
interpretOperation Minus (IntValue left) (IntValue right) = IntValue $ left - right
interpretOperation Multiply (IntValue left) (IntValue right) = IntValue $ left * right
interpretOperation Divide (IntValue left) (IntValue right) = IntValue $ left `div` right
interpretOperation Modulus (IntValue left) (IntValue right) = IntValue $ left `mod` right
interpretOperation Lt (IntValue left) (IntValue right) = BoolValue $ left < right
interpretOperation Gt (IntValue left) (IntValue right) = BoolValue $ left > right
interpretOperation Le (IntValue left) (IntValue right) = BoolValue $ left <= right
interpretOperation Ge (IntValue left) (IntValue right) = BoolValue $ left >= right
interpretOperation Eq (IntValue left) (IntValue right) = BoolValue $ left == right
interpretOperation Neq (IntValue left) (IntValue right) = BoolValue $ left /= right
-- Float
interpretOperation Plus (FloatValue left) (FloatValue right) = FloatValue $ left + right
interpretOperation Minus (FloatValue left) (FloatValue right) = FloatValue $ left - right
Expand Down
30 changes: 23 additions & 7 deletions src/Parser/Expression.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module Parser.Expression (module Parser.Expression) where

import AST
-- import Parser.Atomic
import Parser.Literal (parseLiteral)
import Parser.Name
import Parser.Operator (parseOperator)
Expand All @@ -11,8 +10,16 @@ import Text.Parsec.String

parseExpression :: Parser Expression
parseExpression =
try parseOperation
<|> try parseAtomicExpression
try parseExpression'
<|> try
( char '('
*> spaces'
*> parseExpression'
<* spaces'
<* char ')'
)
where
parseExpression' = try parseOperation <|> try parseAtomicExpression

parseOperation :: Parser Expression
parseOperation =
Expand All @@ -24,8 +31,17 @@ parseOperation =
OperationExpression left op <$> try parseExpression

parseAtomicExpression :: Parser Expression
parseAtomicExpression = do
AtomicExpression <$> try parseAtomic
parseAtomicExpression =
try parseAtomic'
<|> try
( char '('
*> spaces'
*> parseAtomic'
<* spaces'
<* char ')'
)
where
parseAtomic' = AtomicExpression <$> try parseAtomic

parseFunctionCallAtomic :: Parser Atomic
parseFunctionCallAtomic = do
Expand All @@ -37,6 +53,6 @@ parseFunctionCallAtomic = do

parseAtomic :: Parser Atomic
parseAtomic =
(LiteralAtomic <$> try parseLiteral)
LiteralAtomic <$> try parseLiteral
<|> try parseFunctionCallAtomic
<|> (VariableAtomic <$> try parseName)
<|> VariableAtomic <$> try parseName
28 changes: 14 additions & 14 deletions src/Parser/Operator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import Text.Parsec.String

parseOperator :: Parser Operator
parseOperator =
(string "+" >> return Plus)
<|> (string "-" >> return Minus)
<|> (string "*" >> return Multiply)
<|> (string "/" >> return Divide)
<|> (string "%" >> return Modulus)
<|> (string "&&" >> return And)
<|> (string "||" >> return Or)
<|> (string "!" >> return Not)
<|> (string "==" >> return Eq)
<|> (string "!=" >> return Neq)
<|> (string "<" >> return Lt)
<|> (string ">" >> return Gt)
<|> (string "<=" >> return Le)
<|> (string ">=" >> return Ge)
try (string "+" >> return Plus)
<|> try (string "<=" >> return Le)
<|> try (string ">=" >> return Ge)
<|> try (string "-" >> return Minus)
<|> try (string "*" >> return Multiply)
<|> try (string "/" >> return Divide)
<|> try (string "%" >> return Modulus)
<|> try (string "&&" >> return And)
<|> try (string "||" >> return Or)
<|> try (string "==" >> return Eq)
<|> try (string "!=" >> return Neq)
<|> try (string "!" >> return Not)
<|> try (string "<" >> return Lt)
<|> try (string ">" >> return Gt)
2 changes: 1 addition & 1 deletion test/Unit/Parser/Atomic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ allTests =
]

emptyTestAtomic :: Atomic
emptyTestAtomic = (LiteralAtomic (UnitLiteral))
emptyTestAtomic = LiteralAtomic UnitLiteral

testSimple :: Test
testSimple = TestCase $ do
Expand Down
32 changes: 32 additions & 0 deletions test/Unit/Parser/Expression.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,35 @@ testSimple = TestCase $ do
"Function Call"
(AtomicExpression (FunctionCallAtomic "print" []))
(fromRight emptyTestExpression (parse parseExpression "" "print()"))
assertEqual
"(print())"
(AtomicExpression (FunctionCallAtomic "print" []))
(fromRight emptyTestExpression (parse parseExpression "" "(print())"))
assertEqual
"(1) + (3)"
(OperationExpression (AtomicExpression (LiteralAtomic (IntLiteral 1))) Plus (AtomicExpression (LiteralAtomic (IntLiteral 3))))
(fromRight emptyTestExpression (parse parseExpression "" "(1) + (3)"))
assertEqual
"1 + (3 + 4)"
( OperationExpression
(AtomicExpression (LiteralAtomic (IntLiteral 1)))
Plus
( OperationExpression
(AtomicExpression (LiteralAtomic (IntLiteral 3)))
Plus
(AtomicExpression (LiteralAtomic (IntLiteral 4)))
)
)
(fromRight emptyTestExpression (parse parseExpression "" "1 + (3 + 4)"))
assertEqual
"0 == (i % 3)"
( OperationExpression
(AtomicExpression (LiteralAtomic (IntLiteral 0)))
Eq
( OperationExpression
(AtomicExpression (VariableAtomic "i"))
Modulus
(AtomicExpression (LiteralAtomic (IntLiteral 3)))
)
)
(fromRight emptyTestExpression (parse parseExpression "" "0 == (i % 3)"))

0 comments on commit 0a61104

Please sign in to comment.