all repos — min @ ac120ad6b8f2d15a3f06a327ffd979251c0be04b

A small but practical concatenative programming language.

Added interpret method.
h3rald h3rald@h3rald.com
Sun, 06 Nov 2016 11:29:16 +0100
commit

ac120ad6b8f2d15a3f06a327ffd979251c0be04b

parent

6cdda03069cd0d2a3ab5cc0f8a14e77e6b1ea7a1

2 files changed, 20 insertions(+), 11 deletions(-)

jump to
M core/interpreter.nimcore/interpreter.nim

@@ -46,7 +46,7 @@ var scope = newScopeRef(i.scope)

i.withScope(q, scope): body -proc newMinInterpreter*(debugging = false): MinInterpreter = +proc newMinInterpreter*(debugging = false, filename = "input", pwd = ""): MinInterpreter = var stack:MinStack = newSeq[MinValue](0) var trace:MinStack = newSeq[MinValue](0) var stackcopy:MinStack = newSeq[MinValue](0)

@@ -54,8 +54,8 @@ var pr:MinParser

var scope = new MinScope scope.name = "ROOT" var i:MinInterpreter = MinInterpreter( - filename: "input", - pwd: "", + filename: filename, + pwd: pwd, parser: pr, stack: stack, trace: trace,
M minim.nimminim.nim

@@ -116,11 +116,10 @@ if not MINIMRC.fileExists:

MINIMRC.writeFile("") i.eval MINIMRC.readFile() -proc minimStream(s: Stream, filename: string, debugging = false, init = proc(i: In) {.closure, locks:0.}= discard) = +proc minimStream(s: Stream, filename: string, debugging = false) = var i = newMinInterpreter(debugging) i.pwd = filename.parentDir i.stdLib() - i.init() i.open(s, filename) discard i.parser.getToken() try:

@@ -129,22 +128,32 @@ except:

discard i.close() -proc minimString*(buffer: string, debugging = false, init = proc(i: In) {.closure, locks:0.}= discard) = - minimStream(newStringStream(buffer), "input", debugging, init) +proc interpret*(i: In, s: Stream) = + i.stdLib() + i.open(s, i.filename) + discard i.parser.getToken() + try: + i.interpret() + except: + discard + i.close() + +proc minimString*(buffer: string, debugging = false) = + minimStream(newStringStream(buffer), "input", debugging) -proc minimFile*(filename: string, debugging = false, init = proc(i: In) {.closure, locks:0.}= discard) = +proc minimFile*(filename: string, debugging = false) = var stream = newFileStream(filename, fmRead) if stream == nil: stderr.writeLine("Error - Cannot read from file: "& filename) stderr.flushFile() - minimStream(stream, filename, debugging, init) + minimStream(stream, filename, debugging) -proc minimFile*(file: File, filename="stdin", debugging = false, init = proc(i: In) {.closure, locks:0.}= discard) = +proc minimFile*(file: File, filename="stdin", debugging = false) = var stream = newFileStream(stdin) if stream == nil: stderr.writeLine("Error - Cannot read from "& filename) stderr.flushFile() - minimStream(stream, filename, debugging, init) + minimStream(stream, filename, debugging) proc printResult(i: In, res: MinValue) = if res.isNil: