Skip to content

Commit

Permalink
pass string into print function
Browse files Browse the repository at this point in the history
  • Loading branch information
antonkesy committed Mar 7, 2024
1 parent 47fe76e commit ec4adc9
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 47 deletions.
2 changes: 1 addition & 1 deletion app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ developProgram =
-- "int i = 1; int j = 2; int l = 3 + 4; int k = i + j + l; k = k * 0;"
-- "void test(int i, int k) { }"
-- "print()"
"int main() { print(); }"
"int main() { print(\"Test\"); }"

main :: IO ()
main = do
Expand Down
1 change: 1 addition & 0 deletions peter.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ library
AST
Interpreter.BuiltIn
Interpreter.Interpreter
Interpreter.ProgramState
Interpreter.Validator
Parser.Assignment
Parser.Atomic
Expand Down
6 changes: 3 additions & 3 deletions src/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ type Name = String
data Operator = Plus | Minus | Multiply | Divide | Modulus | And | Or | Not | Eq | Neq | Lt | Gt | Le | Ge
deriving (Show, Eq)

data Literal = IntLiteral Int | FloatLiteral Float | BoolLiteral Bool | UnitLiteral
data Literal = IntLiteral Int | FloatLiteral Float | BoolLiteral Bool | UnitLiteral | StringLiteral String
deriving (Show, Eq)

data Atomic = LiteralAtomic Literal | VariableAtomic Name | FunctionCallAtomic Name [Atomic]
data Atomic = LiteralAtomic Literal | VariableAtomic Name | FunctionCallAtomic Name [Expression]
deriving (Show, Eq)

data Expression = OperationExpression Expression Operator Expression | AtomicExpression Atomic
Expand All @@ -25,7 +25,7 @@ data Assignment = Assignment Name Expression

type Comment = String

data Type = IntType | FloatType | BoolType | UnitType | CustomType Name
data Type = IntType | FloatType | BoolType | UnitType | CustomType Name | StringType
deriving (Show, Eq)

data Statement = VariableStatement Variable | AssignmentStatement Assignment | FunctionDefinitionStatement Function | ExpressionStatement Expression
Expand Down
17 changes: 7 additions & 10 deletions src/Interpreter/BuiltIn.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ module Interpreter.BuiltIn (module Interpreter.BuiltIn) where

import AST
import Data.Map.Strict as Map
import Interpreter.ProgramState

-- import Text.Parsec
-- import Text.Parsec.String

-- printBuiltIn :: Parser Statement

data BuiltIn = BuiltIn Name [Type] Type ([Type] -> IO Type)
data BuiltIn = BuiltIn Name Type ([Value] -> IO Value)

getAllBuiltIns :: Map String BuiltIn
getAllBuiltIns = Map.fromList [("print", printBuiltIn)]
Expand All @@ -17,9 +13,10 @@ printBuiltIn :: BuiltIn
printBuiltIn =
BuiltIn
"print"
[CustomType "String"]
UnitType
( \[CustomType "String"] -> do
putStrLn "Hello, World!\n"
pure UnitType
( \val -> do
case val of
[(StringValue s)] -> putStrLn s
_ -> error "Not a single string"
pure UnitValue
)
19 changes: 7 additions & 12 deletions src/Interpreter/Interpreter.hs
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
{-# LANGUAGE GADTs #-}

module Interpreter.Interpreter (module Interpreter.Interpreter) where

import AST
import Control.Monad (foldM)
import Data.Map.Strict as Map
import Interpreter.BuiltIn
import Interpreter.ProgramState
import Interpreter.Validator

data Value = IntValue Int | FloatValue Float | BoolValue Bool | UnitValue
deriving (Show)

data ProgramState where
ProgramState :: {variables :: Map Name Value, functions :: Map Name Statement} -> ProgramState
deriving (Show)

interpret :: Program -> IO ()
interpret (Program statements) = do
isValid <- validate (Program statements)
Expand Down Expand Up @@ -69,11 +61,12 @@ interpretAtomic (ProgramState vars _) (VariableAtomic name) = do
return $ case varValue of
Just value -> value
Nothing -> error $ "Variable not found: " ++ name
interpretAtomic (ProgramState vars funs) (FunctionCallAtomic name _args) = do
interpretAtomic (ProgramState vars funs) (FunctionCallAtomic name args) = do
let isBuiltIn = Map.lookup name getAllBuiltIns
case isBuiltIn of
Just (BuiltIn _ args outputType fn) -> do
_ <- fn args
Just (BuiltIn _ outputType fn) -> do
argValues <- mapM (\a -> interpretExpression (ProgramState vars funs) a) args
_ <- fn argValues
return UnitValue
Nothing -> do
let fun = Map.lookup name funs
Expand All @@ -92,6 +85,8 @@ interpretLiteral (BoolLiteral value) = do
return $ BoolValue value
interpretLiteral UnitLiteral = do
return UnitValue
interpretLiteral (StringLiteral value) = do
return $ StringValue value

interpretOperation :: Operator -> Value -> Value -> Value
interpretOperation Plus (IntValue left) (IntValue right) = IntValue $ left + right
Expand Down
13 changes: 13 additions & 0 deletions src/Interpreter/ProgramState.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{-# LANGUAGE GADTs #-}

module Interpreter.ProgramState (module Interpreter.ProgramState) where

import AST
import Data.Map.Strict as Map

data Value = IntValue Int | FloatValue Float | BoolValue Bool | UnitValue | StringValue String
deriving (Show)

data ProgramState where
ProgramState :: {variables :: Map Name Value, functions :: Map Name Statement} -> ProgramState
deriving (Show)
26 changes: 6 additions & 20 deletions src/Parser/Atomic.hs
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
module Parser.Atomic (module Parser.Atomic) where

import AST
import Parser.Literal (parseLiteral)
import Parser.Space
import Parser.Type
import Text.Parsec
import Text.Parsec.String

parseAtomic :: Parser Atomic
parseAtomic =
(LiteralAtomic <$> try parseLiteral)
<|> try parseFunctionCallAtomic
<|> (VariableAtomic <$> try parseVariableName)

parseFunctionCallAtomic :: Parser Atomic
parseFunctionCallAtomic = do
name <- try parseVariableName
_ <- char '('
args <- try (parseAtomic `sepBy` (spaces' >> char ',' >> spaces'))
_ <- char ')'
return $ FunctionCallAtomic name args
-- import AST
-- import Parser.Literal (parseLiteral)
-- import Parser.Space
-- import Parser.Type
-- import Text.Parsec
-- import Text.Parsec.String
16 changes: 15 additions & 1 deletion src/Parser/Expression.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Parser.Expression (module Parser.Expression) where

import AST
import Parser.Atomic
-- import Parser.Atomic
import Parser.Literal (parseLiteral)
import Parser.Operator (parseOperator)
import Parser.Space
Expand All @@ -26,3 +26,17 @@ parseOperation =
parseAtomicExpression :: Parser Expression
parseAtomicExpression = do
AtomicExpression <$> try parseAtomic

parseFunctionCallAtomic :: Parser Atomic
parseFunctionCallAtomic = do
name <- try parseVariableName
_ <- char '('
args <- try (parseExpression `sepBy` (spaces' >> char ',' >> spaces'))
_ <- char ')'
return $ FunctionCallAtomic name args

parseAtomic :: Parser Atomic
parseAtomic =
(LiteralAtomic <$> try parseLiteral)
<|> try parseFunctionCallAtomic
<|> (VariableAtomic <$> try parseVariableName)
8 changes: 8 additions & 0 deletions src/Parser/Literal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ parseLiteral =
<|> try parseBoolLiteral
<|> try parseFloatLiteral
<|> try parseIntLiteral
<|> try praseStringLiteral

parseIntLiteral :: Parser Literal
parseIntLiteral = do
Expand All @@ -36,3 +37,10 @@ parseBoolLiteral =

parseUnitLiteral :: Parser Literal
parseUnitLiteral = string "()" >> return UnitLiteral

praseStringLiteral :: Parser Literal
praseStringLiteral = do
_ <- char '"'
s <- many (noneOf "\"")
_ <- char '"'
return $ StringLiteral s
1 change: 1 addition & 0 deletions src/Parser/Type.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ parseType =
<|> (string "int" >> return IntType)
<|> (string "float" >> return FloatType)
<|> (string "bool" >> return BoolType)
<|> (string "str" >> return StringType)
<|> (CustomType <$> parseVariableName)

parseVariableName :: Parser Name
Expand Down

0 comments on commit ec4adc9

Please sign in to comment.