Implemented basic I/O commands, started implementing OS commands.
h3rald h3rald@h3rald.com
Sat, 29 Nov 2014 23:00:39 +0100
3 files changed,
71 insertions(+),
10 deletions(-)
M
minim.nim
→
minim.nim
@@ -1,5 +1,5 @@
import streams, tables, parseopt2 -import parser, interpreter, primitives +import parser, interpreter, primitives, utils const version* = "0.1.0"@@ -66,9 +66,13 @@ s.writeln(line)
i.parser.buf = $i.parser.buf & line i.parser.bufLen = i.parser.buf.len discard i.parser.getToken() - i.interpret() - stdout.write "-> " - echo i.dump + try: + i.interpret() + except: + warn getCurrentExceptionMsg() + finally: + stdout.write "-> " + echo i.dump ###
M
primitives.nim
→
primitives.nim
@@ -1,4 +1,4 @@
-import tables, strutils +import tables, strutils, os import parser, interpreter, utils minsym "exit":@@ -29,11 +29,6 @@ let a = i.pop
let b = i.pop i.push a i.push b - -minsym "print": - let a = i.peek - a.print - echo "" # Operations on quotations@@ -319,6 +314,64 @@ i.push newVal(a.boolVal xor b.boolVal)
else: i.error(errIncorrect, "Two bool values are required on the stack") +# I/O + +minsym "puts": + let a = i.peek + echo a + +minsym "gets": + i.push newVal(stdin.readLine()) + +minsym "print": + let a = i.peek + a.print + +minsym "read": + let a = i.pop + if a.isString: + if a.strVal.fileExists: + try: + i.push newVal(a.strVal.readFile) + except: + warn getCurrentExceptionMsg() + else: + warn "File '$1' not found" % [a.strVal] + else: + i.error(errIncorrect, "A string is required on the stack") + +minsym "write": + let a = i.pop + let b = i.pop + if a.isString and b.isString: + try: + a.strVal.writeFile(b.strVal) + except: + warn getCurrentExceptionMsg() + else: + i.error(errIncorrect, "Two strings are required on the stack") + +# OS + +minsym "pwd": + i.push newVal(getCurrentDir()) + +minsym "ls": + let a = i.pop + var list = newSeq[TMinValue](0) + if a.isString: + if a.strVal.existsDir: + for i in walkdir(a.strVal): + list.add newVal(i.path) + i.push list.newVal + else: + warn "Directory '$1' not found" % [a.strVal] + else: + i.error(errIncorrect, "A string is required on the stack") + + +# Aliases + minalias "quit", "exit" minalias "&", "concat" minalias "%", "print"@@ -329,3 +382,4 @@ minalias "gt", ">"
minalias "lt", "<" minalias "gte", ">=" minalias "lte", "<=" +minalias "echi", "puts"