all repos — min @ 3f8feb51f1cf499bafa53914401302969509f56f

A small but practical concatenative programming language.

refactor(validation) Started implementing validation rules.
h3rald h3rald@h3rald.com
Sat, 04 Jun 2016 21:15:01 +0200
commit

3f8feb51f1cf499bafa53914401302969509f56f

parent

115d188bde6f2d21b5764bad991f894b9f785d13

4 files changed, 40 insertions(+), 32 deletions(-)

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

@@ -14,7 +14,8 @@ "Symbol undefined",

"Incorrect items on the stack", "Runtime error", "Two numbers are required on the stack", - "Division by zero" + "Division by zero", + "Two quotations are required on the stack" ] var ROOT*: ref MinScope = new MinScope

@@ -111,6 +112,14 @@ else:

stderr.writeLine("$1 [$2,$3] `$4`: Error - $5" % [i.currSym.filename, $i.currSym.line, $i.currSym.column, i.currSym.symVal, msg]) quit(int(status)) +template execute(i: In, body: stmt) {.immediate.}= + try: + body + except MinRuntimeError: + stderr.writeLine("$1 [$2,$3]: $4" % [i.currSym.filename, $i.currSym.line, $i.currSym.column, getCurrentExceptionMsg()]) + except: + i.error(errSystem, getCurrentExceptionMsg()) + proc open*(i: In, stream:Stream, filename: string) = i.filename = filename i.parser.open(stream, filename)

@@ -130,12 +139,8 @@ if symbolProc.isNotNil:

if i.unsafe: symbolProc(i) else: - try: - symbolProc(i) - except MinRuntimeError: - stderr.writeLine("$1 [$2,$3]: $4" % [i.currSym.filename, $i.currSym.line, $i.currSym.column, getCurrentExceptionMsg()]) - except: - i.error(errSystem, getCurrentExceptionMsg()) + i.execute: + i.symbolProc else: let sigilProc = i.scope.getSigil(sigil) if symbol.len > 1 and sigilProc.isNotNil:

@@ -144,12 +149,8 @@ i.stack.add(MinValue(kind: minString, strVal: sym))

if i.unsafe: sigilProc(i) else: - try: - sigilProc(i) - except MinRuntimeError: - stderr.writeLine("$1 [$2,$3]: $4" % [i.currSym.filename, $i.currSym.line, $i.currSym.column, getCurrentExceptionMsg()]) - except: - i.error(errSystem, getCurrentExceptionMsg()) + i.execute: + i.sigilProc else: i.error(errUndefined, "Undefined symbol: '"&val.symVal&"'") return

@@ -175,10 +176,8 @@

proc interpret*(i: In) = var val: MinValue while i.parser.token != tkEof: - try: + i.execute: val = i.parser.parseMinValue - except: - i.error errParser, getCurrentExceptionMsg() i.push val proc unquote*(i: In, name: string, q: var MinValue) =
M core/types.nimcore/types.nim

@@ -68,10 +68,6 @@ kind*: MinEventKind

err*: MinParserError filename*: string MinStack* = seq[MinValue] - MinParsingError* = ref object of ValueError - MinUndefinedError* = ref object of ValueError - MinRuntimeError* = ref object of SystemError - qVal*: seq[MinValue] MinInterpreter* = object stack*: MinStack pwd*: string

@@ -95,7 +91,13 @@ errUndefined,

errIncorrect, errRuntime, errTwoNumbersRequired, - errDivisionByZero + errDivisionByZero, + errTwoQuotationsRequired + MinParsingError* = ref object of ValueError + MinUndefinedError* = ref object of ValueError + MinInvalidError* = ref object of ValueError + MinRuntimeError* = ref object of SystemError + qVal*: seq[MinValue] proc isNotNil*[T](obj: T): bool = return not obj.isNil
M core/utils.nimcore/utils.nim

@@ -88,3 +88,14 @@

template alias*[T](varname: untyped, value: var T) = var varname {.inject.}: type(value) shallowCopy varname, value + +proc reqTwoQuotations*(i: var MinInterpreter, a, b: var MinValue) = + a = i.pop + b = i.pop + if not a.isQuotation or not b.isQuotation: + raise MinInvalidError(msg: "Two quotations are required on the stack") + +proc reqQuotation*(i: var MinInterpreter, a: var MinValue) = + a = i.pop + if not a.isQuotation: + raise MinInvalidError(msg: "A quotation is required on the stack")
M lib/stack.nimlib/stack.nim

@@ -19,9 +19,8 @@ .symbol("dup") do (i: In):

i.push i.peek .symbol("dip") do (i: In): - var q = i.pop - if not q.isQuotation: - i.error errNoQuotation + var q: MinValue + i.reqQuotation q let v = i.pop i.unquote("<dip>", q) i.push v

@@ -33,14 +32,11 @@ i.push a

i.push b .symbol("sip") do (i: In): - var a = i.pop - let b = i.pop - if a.isQuotation and b.isQuotation: - i.push b - i.unquote("<sip>", a) - i.push b - else: - i.error(errIncorrect, "Two quotations are required on the stack") + var a, b: MinValue + i.reqTwoQuotations a, b + i.push b + i.unquote("<sip>", a) + i.push b .finalize()