diff options
author | Camil Staps | 2016-04-27 10:37:13 +0200 |
---|---|---|
committer | Camil Staps | 2016-04-27 10:37:13 +0200 |
commit | 1af150e798c1580be42bc9e8f3880edbeb6fef29 (patch) | |
tree | 7f924f98de03ab054481f2050e5fd551a4dc9855 | |
parent | Initial commit (diff) |
Nicer cli
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | Makefile | 11 | ||||
-rw-r--r-- | README.md | 10 | ||||
-rw-r--r-- | Smurf.dcl | 13 | ||||
-rw-r--r-- | Smurf.icl | 21 | ||||
-rw-r--r-- | hello.smf | 1 | ||||
-rw-r--r-- | run.icl | 82 | ||||
-rw-r--r-- | run.prj | 60 | ||||
-rw-r--r-- | test.icl | 34 | ||||
-rw-r--r-- | test.prj | 508 |
10 files changed, 191 insertions, 551 deletions
@@ -1,7 +1,7 @@ # Executables *.exe *.out -test +run # Directory used to store object files, abc files and assembly files Clean System Files/ @@ -1,17 +1,14 @@ CPM=cpm -TEST=test +OBJ=run DEPS=Smurf.dcl Smurf.icl SmurfParse.dcl SmurfParse.icl -all: $(TEST) +all: $(OBJ) -$(TEST): %: %.icl $(DEPS) +$(OBJ): %: %.icl $(DEPS) $(CPM) project $@.prj build -run_test: $(TEST) - ./$^ - clean: - rm -fvr $(TEST) Clean\ System\ Files + rm -fvr $(OBJ) Clean\ System\ Files .PHONY: all clean run_test @@ -1,6 +1,16 @@ # CleanSmurf Smurf interpreter in Clean +## Installation + + $ make + +## Usage + + $ ./run -i hello.smf + +There are more command line flags, see `./run -?`. + ## Copyright & license Copyright © 2016 Camil Staps. Licensed under MIT, see LICENSE. @@ -1,6 +1,12 @@ definition module Smurf -from StdOverloaded import class zero, class toString +from StdOverloaded import + class zero, + class toString, + class toChar, class fromChar, + class == + +from GenEq import generic gEq from Data.Maybe import ::Maybe @@ -18,7 +24,12 @@ from Data.Maybe import ::Maybe , store :: Store } +derive gEq Stm + +instance == Stm instance toString Stm +instance toChar Stm +instance fromChar Stm instance zero State instance toString State @@ -12,8 +12,14 @@ import Control.Applicative import Control.Monad import Data.Maybe +import GenEq + import SmurfParse +derive gEq Stm + +instance == Stm where == a b = a === b + instance zero [a] where zero = [] instance toString Stm @@ -29,6 +35,21 @@ where toString Get = "g" toString Exec = "x" +instance toChar Stm where toChar stm = (toString stm).[0] + +instance fromChar Stm +where + fromChar '"' = Push "" + fromChar 'i' = Input + fromChar 'o' = Output + fromChar '+' = Cat + fromChar 'h' = Head + fromChar 't' = Tail + fromChar 'q' = Quotify + fromChar 'p' = Put + fromChar 'g' = Get + fromChar 'x' = Exec + instance zero State where zero = { stack = zero, store = zero } instance toString State diff --git a/hello.smf b/hello.smf new file mode 100644 index 0000000..6d8251f --- /dev/null +++ b/hello.smf @@ -0,0 +1 @@ +"hello ""world"+o @@ -0,0 +1,82 @@ +module run + +import StdEnv, StdDebug + +import GenEq + +import Data.Maybe, Data.List +from Data.Func import $ + +import System.CommandLine, System.GetOpt + +import Smurf +import SmurfParse + +:: SmurfOpt = Break Char | BreakAll | IFile String | Verbose + +derive gEq SmurfOpt +instance == SmurfOpt where == a b = a === b + +Start :: *World -> *World +Start w +// Options +# (cmd, w) = getCommandLine w +# (opts,_,errs) = getOpt Permute options cmd +| errs <> [] = error errs w +# breakstms + = if (isMember BreakAll opts) + [Push "", Input, Output, Cat, Head, Tail, Quotify, Put, Get, Exec] + [fromChar c \\ (Break c) <- opts] +# inputs = [f \\ (IFile f) <- opts] +| length inputs <> 1 = error ["Exactly one input file required"] w +# verbose = isMember Verbose opts +// Read input file +# (_, f, w) = fopen (hd inputs) FReadText w +# (pgm, f) = freads f 100000 +# (_, w) = fclose f w +# mbPgm = parse $ fromString pgm +| isNothing mbPgm = error ["Couldn't parse input file as a Smurf program "] w +# pgm = fromJust mbPgm +// Start interpreting +# (io, w) = stdio w +# io = loop verbose breakstms pgm zero io += snd $ fclose io w +where + loop :: Bool ![Stm] !Program !State !*File -> *File + loop v brk p st f + # p = if v (trace (foldl (+++) "" (map toString p) +++ "\n") p) p + # (mbProgSt, f) = step p st f + | isNothing mbProgSt = f <<< "NOTHING!!!\n" + # (prog, st) = fromJust mbProgSt + | isEmpty prog = f <<< "\n---------------------------\n" <<< toString st + | not (isBrk prog) = loop v brk prog st f + # f = f <<< "---> " <<< toString (hd prog) <<< " ? " + # (cmd, f) = freadline f + | cmd == "" = loop v [] prog st (f <<< "\n") // Ctrl-D: stop breaking + # cmd = cmd % (0, size cmd - 2) + | cmd == "state" = loop v brk prog st (f <<< toString st) // Print state + = loop v brk prog st f + where + isBrk :: Program -> Bool + isBrk [] = False + isBrk [(Push _):_] = isMember (Push "") brk + isBrk [stm:_] = isMember stm brk + + options = [ Option ['i'] ["infile"] + (ReqArg IFile "FILE") "Smurf file" + , Option ['b'] ["break"] + (ReqArg (\s->Break s.[0]) "STM") "Break on statement" + , Option ['B'] ["break-all"] + (NoArg BreakAll) "Break on all statements" + , Option ['v'] ["verbose"] + (NoArg Verbose) "Print all programs to stderr" + ] + + error :: [String] *World -> *World + error errs w + # (io, w) = stdio w + # io = io <<< foldl (+++) "" (intersperse "\n" errs) <<< "\n" + # io = io <<< usageInfo "CleanSmurf: a Clean Smurf interpreter" options + # io = io <<< "\n\n" + = snd $ fclose io w + @@ -0,0 +1,60 @@ +Version: 1.4 +Global + ProjectRoot: . + Target: StdEnv + Exec: {Project}/run + CodeGen + CheckStacks: False + CheckIndexes: True + Application + HeapSize: 2097152 + StackSize: 512000 + ExtraMemory: 8192 + IntialHeapSize: 204800 + HeapSizeMultiplier: 4096 + ShowExecutionTime: False + ShowGC: False + ShowStackSize: False + MarkingCollector: False + DisableRTSFlags: False + StandardRuntimeEnv: True + Profile + Memory: False + MemoryMinimumHeapSize: 0 + Time: False + Stack: False + Output + Output: NoConsole + Font: Monaco + FontSize: 9 + WriteStdErr: False + Link + LinkMethod: Static + GenerateRelocations: False + GenerateSymbolTable: False + GenerateLinkMap: False + LinkResources: False + ResourceSource: + GenerateDLL: False + ExportedNames: + Paths + Path: {Project}/ + Path: {Application}/lib/Generics/ + Path: {Application}/lib/clean-platform/OS-Independent/ + Path: {Application}/lib/clean-platform/OS-Linux-64/ + Precompile: + Postlink: +MainModule + Name: run + Dir: {Project} + Compiler + NeverMemoryProfile: False + NeverTimeProfile: False + StrictnessAnalysis: True + ListTypes: StrictExportTypes + ListAttributes: True + Warnings: True + Verbose: True + ReadableABC: False + ReuseUniqueNodes: True + Fusion: False diff --git a/test.icl b/test.icl deleted file mode 100644 index 77e836c..0000000 --- a/test.icl +++ /dev/null @@ -1,34 +0,0 @@ -module test - -import StdEnv -import Data.Maybe -from Data.Func import $ - -import Smurf -import SmurfParse - -Start :: *World -> *World -Start w -# (io, w) = stdio w -//# io = loop (prog reverse) zero io -# (mbSt, io) = run (prog reverse) zero io -# result = if (isNothing mbSt) "uh-oh." (toString $ fromJust mbSt) -# io = io <<< "\n--------------------\n" <<< result <<< "\n" -= snd $ fclose io w -where - loop :: !Program State !*File -> *File - loop p st f - # (mbProgSt, f) = step p st f - | isNothing mbProgSt = f <<< "NOTHING!!!\n" - # (prog, st) = fromJust mbProgSt - | isEmpty prog = f <<< "\n---------------------------\n" <<< toString st - # f = f <<< "---> " <<< toString (hd prog) <<< " ? " - # (cmd, f) = freadline f - | cmd == "state\n" = loop prog st (f <<< toString st) - = loop prog st f - - prog txt = fromJust $ parse txt - - // From http://esolangs.org/wiki/Smurf - reverse = ['"+"i+""p""gtg""gt"i"p"\\"\\"p\\"i\\"gh\\"o\\"g+\\"o\\"p\\"i\\"gt\\"i\\"p\\"\\\\\\"+\\\\\\"\\\\\\"\\\\\\"p\\"\\"i\\"gq+\\"tg\\"+\\"i\\"gq+\\"\\\\\\"i\\\\\\"p\\"+\\"o\\"gq+\\"\\\\\\"o\\\\\\"p\\"+\\"\\"gq+\\"\\"g+\\"\\"p\\"o\\"gq\\"o\\"+\\"+\\"pgx"""p"\\"+\\"\\"\\"p""i"gq+"tg"+"i"gq+"\\"i\\"p\\"\\""+""gq+""g+""p"""+""i"g+pgx'] - diff --git a/test.prj b/test.prj deleted file mode 100644 index 00ff6fb..0000000 --- a/test.prj +++ /dev/null @@ -1,508 +0,0 @@ -Version: 1.4 -Global - ProjectRoot: . - Target: StdEnv - Exec: {Project}/test - CodeGen - CheckStacks: False - CheckIndexes: True - Application - HeapSize: 2097152 - StackSize: 512000 - ExtraMemory: 8192 - IntialHeapSize: 204800 - HeapSizeMultiplier: 4096 - ShowExecutionTime: False - ShowGC: False - ShowStackSize: False - MarkingCollector: False - DisableRTSFlags: False - StandardRuntimeEnv: True - Profile - Memory: False - MemoryMinimumHeapSize: 0 - Time: False - Stack: False - Output - Output: NoConsole - Font: Monaco - FontSize: 9 - WriteStdErr: False - Link - LinkMethod: Static - GenerateRelocations: False - GenerateSymbolTable: False - GenerateLinkMap: False - LinkResources: False - ResourceSource: - GenerateDLL: False - ExportedNames: - Paths - Path: {Project}/ - Path: {Application}/lib/Generics/ - Path: {Application}/lib/clean-platform/OS-Independent/ - Precompile: - Postlink: -MainModule - Name: test - Dir: {Project} - Compiler - NeverMemoryProfile: False - NeverTimeProfile: False - StrictnessAnalysis: True - ListTypes: StrictExportTypes - ListAttributes: True - Warnings: True - Verbose: True - ReadableABC: False - ReuseUniqueNodes: True - Fusion: False -OtherModules - Module - Name: Smurf - Dir: {Project}/ - Compiler - NeverMemoryProfile: False - NeverTimeProfile: False - StrictnessAnalysis: True - ListTypes: StrictExportTypes - ListAttributes: True - Warnings: True - Verbose: True - ReadableABC: False - ReuseUniqueNodes: True - Fusion: False - Module - Name: SmurfParse - Dir: {Project}/ - Compiler - NeverMemoryProfile: False - NeverTimeProfile: False - StrictnessAnalysis: True - ListTypes: StrictExportTypes - ListAttributes: True - Warnings: True - Verbose: True - ReadableABC: False - ReuseUniqueNodes: True - Fusion: False - Module - Name: GenEq - Dir: {Application}/lib/Generics/ - Compiler - NeverMemoryProfile: False - NeverTimeProfile: False - StrictnessAnalysis: True - ListTypes: StrictExportTypes - ListAttributes: True - Warnings: True - Verbose: True - ReadableABC: False - ReuseUniqueNodes: True - Fusion: False - Module - Name: Control.Applicative - Dir: {Application}/lib/clean-platform/OS-Independent/ - Compiler - NeverMemoryProfile: False - NeverTimeProfile: False - StrictnessAnalysis: True - ListTypes: StrictExportTypes - ListAttributes: True - Warnings: True - Verbose: True - ReadableABC: False - ReuseUniqueNodes: True - Fusion: False - Module - Name: Control.Monad - Dir: {Application}/lib/clean-platform/OS-Independent/ - Compiler - NeverMemoryProfile: False - NeverTimeProfile: False - StrictnessAnalysis: True - ListTypes: StrictExportTypes - ListAttributes: True - Warnings: True - Verbose: True - ReadableABC: False - ReuseUniqueNodes: True - Fusion: False - Module - Name: Data.Func - Dir: {Application}/lib/clean-platform/OS-Independent/ - Compiler - NeverMemoryProfile: False - NeverTimeProfile: False - StrictnessAnalysis: True - ListTypes: StrictExportTypes - ListAttributes: True - Warnings: True - Verbose: True - ReadableABC: False - ReuseUniqueNodes: True - Fusion: False - Module - Name: Data.Functor - Dir: {Application}/lib/clean-platform/OS-Independent/ - Compiler - NeverMemoryProfile: False - NeverTimeProfile: False - StrictnessAnalysis: True - ListTypes: StrictExportTypes - ListAttributes: True - Warnings: True - Verbose: True - ReadableABC: False - ReuseUniqueNodes: True - Fusion: False - Module - Name: Data.List - Dir: {Application}/lib/clean-platform/OS-Independent/ - Compiler - NeverMemoryProfile: False - NeverTimeProfile: False - StrictnessAnalysis: True - ListTypes: StrictExportTypes - ListAttributes: True - Warnings: True - Verbose: True - ReadableABC: False - ReuseUniqueNodes: True - Fusion: False - Module - Name: Data.Maybe - Dir: {Application}/lib/clean-platform/OS-Independent/ - Compiler - NeverMemoryProfile: False - NeverTimeProfile: False - StrictnessAnalysis: True - ListTypes: StrictExportTypes - ListAttributes: True - Warnings: True - Verbose: True - ReadableABC: False - ReuseUniqueNodes: True - Fusion: False - Module - Name: Data.Monoid - Dir: {Application}/lib/clean-platform/OS-Independent/ - Compiler - NeverMemoryProfile: False - NeverTimeProfile: False - StrictnessAnalysis: True - ListTypes: StrictExportTypes - ListAttributes: True - Warnings: True - Verbose: True - ReadableABC: False - ReuseUniqueNodes: True - Fusion: False - Module - Name: Data.Void - Dir: {Application}/lib/clean-platform/OS-Independent/ - Compiler - NeverMemoryProfile: False - NeverTimeProfile: False - StrictnessAnalysis: True - ListTypes: StrictExportTypes - ListAttributes: True - Warnings: True - Verbose: True - ReadableABC: False - ReuseUniqueNodes: True - Fusion: False - Module - Name: System.IO - Dir: {Application}/lib/clean-platform/OS-Independent/ - Compiler - NeverMemoryProfile: False - NeverTimeProfile: False - StrictnessAnalysis: True - ListTypes: StrictExportTypes - ListAttributes: True - Warnings: True - Verbose: True - ReadableABC: False - ReuseUniqueNodes: True - Fusion: False - Module - Name: StdArray - Dir: {Application}/lib/StdEnv - Compiler - NeverMemoryProfile: False - NeverTimeProfile: False - StrictnessAnalysis: True - ListTypes: StrictExportTypes - ListAttributes: True - Warnings: True - Verbose: True - ReadableABC: False - ReuseUniqueNodes: True - Fusion: False - Module - Name: StdBool - Dir: {Application}/lib/StdEnv - Compiler - NeverMemoryProfile: False - NeverTimeProfile: False - StrictnessAnalysis: True - ListTypes: StrictExportTypes - ListAttributes: True - Warnings: True - Verbose: True - ReadableABC: False - ReuseUniqueNodes: True - Fusion: False - Module - Name: StdChar - Dir: {Application}/lib/StdEnv - Compiler - NeverMemoryProfile: False - NeverTimeProfile: False - StrictnessAnalysis: True - ListTypes: StrictExportTypes - ListAttributes: True - Warnings: True - Verbose: True - ReadableABC: False - ReuseUniqueNodes: True - Fusion: False - Module - Name: StdCharList - Dir: {Application}/lib/StdEnv - Compiler - NeverMemoryProfile: False - NeverTimeProfile: False - StrictnessAnalysis: True - ListTypes: StrictExportTypes - ListAttributes: True - Warnings: True - Verbose: True - ReadableABC: False - ReuseUniqueNodes: True - Fusion: False - Module - Name: StdClass - Dir: {Application}/lib/StdEnv - Compiler - NeverMemoryProfile: False - NeverTimeProfile: False - StrictnessAnalysis: True - ListTypes: StrictExportTypes - ListAttributes: True - Warnings: True - Verbose: True - ReadableABC: False - ReuseUniqueNodes: True - Fusion: False - Module - Name: StdEnum - Dir: {Application}/lib/StdEnv - Compiler - NeverMemoryProfile: False - NeverTimeProfile: False - StrictnessAnalysis: True - ListTypes: StrictExportTypes - ListAttributes: True - Warnings: True - Verbose: True - ReadableABC: False - ReuseUniqueNodes: True - Fusion: False - Module - Name: StdEnv - Dir: {Application}/lib/StdEnv - Compiler - NeverMemoryProfile: False - NeverTimeProfile: False - StrictnessAnalysis: True - ListTypes: StrictExportTypes - ListAttributes: True - Warnings: True - Verbose: True - ReadableABC: False - ReuseUniqueNodes: True - Fusion: False - Module - Name: StdFile - Dir: {Application}/lib/StdEnv - Compiler - NeverMemoryProfile: False - NeverTimeProfile: False - StrictnessAnalysis: True - ListTypes: StrictExportTypes - ListAttributes: True - Warnings: True - Verbose: True - ReadableABC: False - ReuseUniqueNodes: True - Fusion: False - Module - Name: StdFunc - Dir: {Application}/lib/StdEnv - Compiler - NeverMemoryProfile: False - NeverTimeProfile: False - StrictnessAnalysis: True - ListTypes: StrictExportTypes - ListAttributes: True - Warnings: True - Verbose: True - ReadableABC: False - ReuseUniqueNodes: True - Fusion: False - Module - Name: StdGeneric - Dir: {Application}/lib/StdEnv - Compiler - NeverMemoryProfile: False - NeverTimeProfile: False - StrictnessAnalysis: True - ListTypes: StrictExportTypes - ListAttributes: True - Warnings: True - Verbose: True - ReadableABC: False - ReuseUniqueNodes: True - Fusion: False - Module - Name: StdInt - Dir: {Application}/lib/StdEnv - Compiler - NeverMemoryProfile: False - NeverTimeProfile: False - StrictnessAnalysis: True - ListTypes: StrictExportTypes - ListAttributes: True - Warnings: True - Verbose: True - ReadableABC: False - ReuseUniqueNodes: True - Fusion: False - Module - Name: StdList - Dir: {Application}/lib/StdEnv - Compiler - NeverMemoryProfile: False - NeverTimeProfile: False - StrictnessAnalysis: True - ListTypes: StrictExportTypes - ListAttributes: True - Warnings: True - Verbose: True - ReadableABC: False - ReuseUniqueNodes: True - Fusion: False - Module - Name: StdMisc - Dir: {Application}/lib/StdEnv - Compiler - NeverMemoryProfile: False - NeverTimeProfile: False - StrictnessAnalysis: True - ListTypes: StrictExportTypes - ListAttributes: True - Warnings: True - Verbose: True - ReadableABC: False - ReuseUniqueNodes: True - Fusion: False - Module - Name: StdOrdList - Dir: {Application}/lib/StdEnv - Compiler - NeverMemoryProfile: False - NeverTimeProfile: False - StrictnessAnalysis: True - ListTypes: StrictExportTypes - ListAttributes: True - Warnings: True - Verbose: True - ReadableABC: False - ReuseUniqueNodes: True - Fusion: False - Module - Name: StdOverloaded - Dir: {Application}/lib/StdEnv - Compiler - NeverMemoryProfile: False - NeverTimeProfile: False - StrictnessAnalysis: True - ListTypes: StrictExportTypes - ListAttributes: True - Warnings: True - Verbose: True - ReadableABC: False - ReuseUniqueNodes: True - Fusion: False - Module - Name: StdReal - Dir: {Application}/lib/StdEnv - Compiler - NeverMemoryProfile: False - NeverTimeProfile: False - StrictnessAnalysis: True - ListTypes: StrictExportTypes - ListAttributes: True - Warnings: True - Verbose: True - ReadableABC: False - ReuseUniqueNodes: True - Fusion: False - Module - Name: StdString - Dir: {Application}/lib/StdEnv - Compiler - NeverMemoryProfile: False - NeverTimeProfile: False - StrictnessAnalysis: True - ListTypes: StrictExportTypes - ListAttributes: True - Warnings: True - Verbose: True - ReadableABC: False - ReuseUniqueNodes: True - Fusion: False - Module - Name: StdTuple - Dir: {Application}/lib/StdEnv - Compiler - NeverMemoryProfile: False - NeverTimeProfile: False - StrictnessAnalysis: True - ListTypes: StrictExportTypes - ListAttributes: True - Warnings: True - Verbose: True - ReadableABC: False - ReuseUniqueNodes: True - Fusion: False - Module - Name: _SystemArray - Dir: {Application}/lib/StdEnv - Compiler - NeverMemoryProfile: False - NeverTimeProfile: False - StrictnessAnalysis: True - ListTypes: StrictExportTypes - ListAttributes: True - Warnings: True - Verbose: True - ReadableABC: False - ReuseUniqueNodes: True - Fusion: False - Module - Name: _SystemEnum - Dir: {Application}/lib/StdEnv - Compiler - NeverMemoryProfile: False - NeverTimeProfile: False - StrictnessAnalysis: True - ListTypes: StrictExportTypes - ListAttributes: True - Warnings: True - Verbose: True - ReadableABC: False - ReuseUniqueNodes: True - Fusion: False |