From bad32a12573bf14968746ea9ad0f6c4f20b50cf1 Mon Sep 17 00:00:00 2001 From: Matthew Hall Date: Wed, 22 Feb 2023 22:23:35 +1300 Subject: Re-work program structure, MAIN entrypoint + only PROC definitions --- DSL/Interpretation.hs | 11 ++++++++--- DSL/Parsing.hs | 14 +++----------- DSL/Types.hs | 2 -- 3 files changed, 11 insertions(+), 16 deletions(-) (limited to 'DSL') diff --git a/DSL/Interpretation.hs b/DSL/Interpretation.hs index 37165c3..d9c53bd 100644 --- a/DSL/Interpretation.hs +++ b/DSL/Interpretation.hs @@ -8,6 +8,9 @@ import DSL.Util import DSL.Intrinsics import DSL.StdLib (stdlib) +entrypoint :: ProcName +entrypoint = "MAIN" + newMachine :: Machine newMachine = Machine { ok=True, stack=[], pTable=M.empty } @@ -93,8 +96,10 @@ evalBlocks (BLinear b:bs) m = applyLinear m b >>= evalBlocks bs evalBlocks (BIf c t f:bs) m = applyIf c t f m >>= evalBlocks bs evalBlocks (BWhile c b:bs) m = applyWhile c b m >>= evalBlocks bs -interpret :: (ProcTable, Program) -> IO () -interpret (t, p) = case mergeProcTables stdlib t of - Just t' -> () <$ evalBlocks p newMachine{ pTable=t' } +interpret :: ProcTable -> IO () +interpret t = case mergeProcTables stdlib t of Nothing -> putStrLn "Failed to include stdlib, duplicate proc definition?" + Just t' -> case M.lookup entrypoint t' of + Nothing -> putStrLn "No MAIN proc defined, aborting." + Just b -> () <$ evalBlocks b newMachine{ pTable=t' } diff --git a/DSL/Parsing.hs b/DSL/Parsing.hs index 72d6b23..d4d1253 100644 --- a/DSL/Parsing.hs +++ b/DSL/Parsing.hs @@ -191,16 +191,8 @@ blockP = firstOf [ whileP , linearP ] -programP :: DSLParser ([ProcSpec], Program) -programP = phrase $ procs `plus` code +stringToProgram :: String -> Maybe ProcTable +stringToProgram = (>>=buildProcTable . snd) . parse (tokenizer `chain` program) where - procs = mult procP - code = mult1 blockP - -stringToProgram :: String -> Maybe (ProcTable, Program) -stringToProgram = (>>=(f . snd)) . parse (tokenizer `chain` programP) - where - f (ps, b) = case buildProcTable ps of - Nothing -> Nothing - Just t -> Just (t, b) + program = phrase $ mult procP diff --git a/DSL/Types.hs b/DSL/Types.hs index 7467bfd..aa1fb22 100644 --- a/DSL/Types.hs +++ b/DSL/Types.hs @@ -69,8 +69,6 @@ type ProcSpec = (ProcName, [Block]) type ProcTable = Map ProcName [Block] -type Program = [Block] - data Machine = Machine { ok :: Bool , stack :: Stack , pTable :: ProcTable -- cgit v1.2.1