X-Git-Url: https://git.martlubbers.net/?a=blobdiff_plain;ds=sidebyside;f=gen.icl;h=a37fedf060fc18e27eabf4bf4617e20c4ec8143d;hb=e01254ee33ddd811fe95ea98cba19909f3e4f740;hp=0fc8bc04d522ea29e1d4e9ba349beaef295ca2bd;hpb=552f09db0d73eca583d297abd7114462fd87646d;p=cc1516.git diff --git a/gen.icl b/gen.icl index 0fc8bc0..a37fedf 100644 --- a/gen.icl +++ b/gen.icl @@ -1,14 +1,12 @@ implementation module gen - import StdMisc import StdList import StdOverloaded import StdString -from StdFunc import id +from StdFunc import id, const import StdTuple import StdEnum -from StdEnv import const import Data.Func import qualified Data.Map as Map @@ -26,8 +24,6 @@ from Text import class Text(concat), instance Text String import AST import RWST -//Instruction is an instruction, with possible arguments and a possible comment -//Or is a label TRUE :== -1 FALSE :== 0 :: Instr = Instr String [Arg] String @@ -44,25 +40,14 @@ FALSE :== 0 :: Gen a :== RWST Label SSMProgram (GenMap, [Label]) (Either GenError) a labelStream :: [Label] -labelStream = map (\i-> concat ["lbl_", toString i]) [1..] +labelStream = ["lbl_" +++ toString i\\i<-[1..]] -gen :: AST -> String +gen :: AST -> Either String String gen (AST fds) = case evalRWST prog "" ('Map'.newMap, labelStream) of - Left (Error e) = e - Right (_, p) = toString p + Left (Error e) = Left e + Right (_, p) = Right $ toString p where prog = tell [Instr "bra" [L "main"] ""] >>| mapM_ g fds -//gen _ = prog -// where -// expr = (Op2Expr zero (Op1Expr zero UnMinus (IntExpr zero 4)) BiPlus (IntExpr zero 7)) -// expr2 = (FunExpr zero "test" [IntExpr zero 4] []) -// stmt = (IfStmt (BoolExpr zero True) [] []) -// prog = case evalRWST (g stmt) 0 ('Map'.newMap, labelStream) of -// Left (Error e) = abort e -// Right (_, prog) = toString prog -//gen _ = toString [Lab "Test" -// ,Instr "ldc" [Lit 1] "Eerste instructie" -// ,Instr "ldc" [Lit 2] "Tweede instructie"] //Current issues: //All VarDecls are added as function, how to deal with assignments? @@ -76,6 +61,7 @@ gen (AST fds) = case evalRWST prog "" ('Map'.newMap, labelStream) of //helper functions for the gen monad genMap :: Gen GenMap genMap = gets fst + changeGenMap :: (GenMap -> GenMap) -> Gen GenMap changeGenMap f = modify (appFst f) >>| genMap @@ -218,4 +204,4 @@ instance toString Arg where toString (Raw s) = s instance toString SSMProgram where - toString p = concat $ intersperse " " $ map (\i-> concat $ intersperse " " $ print i) p \ No newline at end of file + toString p = concat $ intersperse " " $ map (\i-> concat $ intersperse " " $ print i) p