@@ -26,7 +26,7 @@ import Foreign
2626import Foreign.C (CString , newCString , peekCStringLen )
2727import Foreign.C.String (peekCString )
2828import Foreign.C.Types (CChar )
29- import Parser (CompilerFlags (CompilerFlags ), initCompilerFlags , parseProgram )
29+ import Parser (CompilerFlags (CompilerFlags ), exprs , initCompilerFlags , parseProgram )
3030import Parser qualified
3131import Text.Megaparsec (errorBundlePretty )
3232import VM
@@ -39,6 +39,7 @@ import VM
3939 , runVM
4040 , runVMVM
4141 )
42+ import Verifier (verifyProgram )
4243
4344-- TODO: Put this stuff into a seperate file
4445runProgramRaw :: Ptr CChar -> Int -> IO ()
@@ -56,21 +57,25 @@ runProgramRaw progPtr progLen = do
5657
5758runProgramRawBuffered :: Ptr CChar -> Int -> Ptr CChar -> Int -> Ptr (Ptr CChar ) -> IO Int
5859runProgramRawBuffered progPtr progLen inputPtr inputLen outputPtrPtr = do
59- program <- peekCStringLen (progPtr, progLen)
60+ programStr <- peekCStringLen (progPtr, progLen)
6061 input <- peekCStringLen (inputPtr, inputLen)
61- let p = parseProgram (Data.Text. pack program ) Parser. initCompilerFlags
62+ let p = parseProgram (Data.Text. pack programStr ) Parser. initCompilerFlags
6263 case p of
6364 Left err -> error $ errorBundlePretty err
6465 Right program -> do
65- xxx <- evalStateT (compileProgram program) (initCompilerState program)
66- let xxxPoint = locateLabel xxx " main"
67- vm <- runVMVM $ (initVM (V. fromList xxx)){pc = xxxPoint, breakpoints = [] , callStack = [StackFrame {returnAddress = xxxPoint, locals = [] }], ioMode = VMBuffer , ioBuffer = IOBuffer {input = input, output = " " }}
68- let output' = BS. pack $ output $ ioBuffer vm
69- BU. unsafeUseAsCStringLen output' $ \ (buf, len) -> do
70- outputPtr <- mallocBytes len
71- poke outputPtrPtr outputPtr
72- copyBytes outputPtr buf len
73- pure len
66+ verified <- verifyProgram " " (Data.Text. pack programStr) (exprs program)
67+ case verified of
68+ Left err -> error $ errorBundlePretty err
69+ Right _ -> do
70+ xxx <- evalStateT (compileProgram program) (initCompilerState program)
71+ let xxxPoint = locateLabel xxx " main"
72+ vm <- runVMVM $ (initVM (V. fromList xxx)){pc = xxxPoint, breakpoints = [] , callStack = [StackFrame {returnAddress = xxxPoint, locals = [] }], ioMode = VMBuffer , ioBuffer = IOBuffer {input = input, output = " " }}
73+ let output' = BS. pack $ output $ ioBuffer vm
74+ BU. unsafeUseAsCStringLen output' $ \ (buf, len) -> do
75+ outputPtr <- mallocBytes len
76+ poke outputPtrPtr outputPtr
77+ copyBytes outputPtr buf len
78+ pure len
7479
7580foreign export ccall mallocPtr :: IO (Ptr (Ptr a ))
7681
0 commit comments