diff options
author | Marvin Borner | 2024-12-28 23:25:18 +0100 |
---|---|---|
committer | Marvin Borner | 2024-12-28 23:25:47 +0100 |
commit | 0e93cc19dcef679e568d40f06d7b1329d3196562 (patch) | |
tree | bbbc2ea2abd1fc79cf3b020b144c7e9125534482 | |
parent | 18ffa77fb4e5343ee9ee4095e499f94bc92b16bb (diff) |
ok
-rw-r--r-- | lllars/SYNTAX | 4 | ||||
-rw-r--r-- | lllars/fac.lll | 2 | ||||
-rw-r--r-- | lllars/parser.hs | 71 |
3 files changed, 69 insertions, 8 deletions
diff --git a/lllars/SYNTAX b/lllars/SYNTAX index 8aa9904..34a2353 100644 --- a/lllars/SYNTAX +++ b/lllars/SYNTAX @@ -63,3 +63,7 @@ lars|sral 10 larssral check ifnot 0 at address 10 if true goto fst sral|lars 10 lars + +CYCLIC REDUNDANCY CHECK: + redundancy: runs in THREE parallel runtimes (evil, haskell comp, kotlin comp) and compare results + cyclic: recompilation from c to lars and back, n times, until 1GB(?) filesize diff --git a/lllars/fac.lll b/lllars/fac.lll index 6502411..787d60d 100644 --- a/lllars/fac.lll +++ b/lllars/fac.lll @@ -1,7 +1,7 @@ !!! all rights reserved to lars <3 !!! lars initial values: @0 is n, @1 is result -0lars50 +0lars10 1lars1 larslars loop diff --git a/lllars/parser.hs b/lllars/parser.hs index 0e828b7..3d1e713 100644 --- a/lllars/parser.hs +++ b/lllars/parser.hs @@ -126,11 +126,11 @@ branch = instr :: Parser Instr instr = try comment <|> write <|> call <|> namedLabel <|> goto <|> branch -preamble :: Parser String -preamble = string "!!! all rights reserved to lars <3 !!!\n\n" +license :: Parser String +license = string "!!! all rights reserved to lars <3 !!!\n\n" program :: Parser Program -program = preamble *> sepEndBy instr (some $ char '\n') +program = license *> sepEndBy instr (some $ char '\n') type EvalState = HashMap Address Int @@ -159,7 +159,6 @@ evalAddressation (BinaryOperation a operator b) = do resB <- evalAccess b return $ evilOperation resA operator resB --- data Instr = Comment String | Write Address Addressation | LarsCall Call | Label Label | GoTo Label | Branch BranchPolarity Address Label eval :: Int -> Program -> State EvalState () eval n p = go (drop n p) where @@ -189,13 +188,71 @@ eval n p = go (drop n p) _ -> go ps go [] = return () +compileOperation :: String -> Operator -> String -> String +compileOperation a ADD b = a <> " + " <> b +compileOperation a MUL b = a <> " * " <> b +compileOperation a SUB b = a <> " - " <> b +compileOperation a DIV b = a <> " / " <> b +compileOperation a AND b = a <> " & " <> b +compileOperation a OR b = a <> " | " <> b +compileOperation a XOR b = a <> " ^ " <> b + +compileAccess :: Access -> String +compileAccess (Access address) = show address +compileAccess (SAccess access ) = "heap[" <> compileAccess access <> "]" + +compileAddressation :: Addressation -> String +compileAddressation (Address access) = compileAccess access +compileAddressation (BinaryOperation a operator b) = + compileOperation (compileAccess a) operator (compileAccess b) + +compile :: Program -> String +compile ((Comment comment) : ps) = "// " <> comment <> "\n" ++ compile ps +compile ((Write address addressation) : ps) = + "heap[" + <> show address + <> "] = " + <> compileAddressation addressation + <> ";\n" + ++ compile ps +compile ((LarsCall call ) : ps) = compile ps -- TODO +compile ((Label label) : ps) = "" <> label <> ":\n" ++ compile ps +compile ((GoTo label) : ps) = "goto " <> label <> ";\n" ++ compile ps +compile ((Branch IfTrue access label) : ps) = + "if (" + <> compileAccess access + <> ")" + <> " goto " + <> label + <> ";\n" + ++ compile ps +compile ((Branch IfFalse access label) : ps) = + "if (!(" + <> compileAccess access + <> "))" + <> " goto " + <> label + <> ";\n" + ++ compile ps +compile [] = "" + +preamble :: String +preamble = + "#include <stdio.h>\nint main(int argc, char *argv[]) { unsigned int heap[10000] = { 0 }; " + +postamble :: String +postamble = "printf(\"%x\\n\", heap[8159]); }" + +preamblify :: String -> String +preamblify amble = preamble ++ amble ++ postamble + main :: IO () main = do f <- readFile "fac.lll" case runParser (program <* many (char '\n') <* eof) "" f of Right ps -> do - print ps - let ev = runState (eval 0 ps) M.empty - print ev + -- let ev = runState (eval 0 ps) M.empty + -- print ev + putStrLn $ preamblify $ compile ps -- putStrLn $ "[" <> intercalate "," (show <$> ps) <> "]" Left err -> putStrLn $ errorBundlePretty err |