all repos — min @ e3170f6ceb89e049f37709fecda01dee526a8990

A small but practical concatenative programming language.

Refactoring: removed hasSymbol method.
h3rald h3rald@h3rald.com
Tue, 18 Jun 2024 10:18:10 +0200
commit

e3170f6ceb89e049f37709fecda01dee526a8990

parent

9d4f6657f39a22f29724f1bcebc4a40b5df4a5f8

4 files changed, 35 insertions(+), 67 deletions(-)

jump to
M minpkg/core/interpreter.nimminpkg/core/interpreter.nim

@@ -279,16 +279,18 @@ let symbol = val.symVal

if symbol == "return": raise MinReturnException(msg: "return symbol found") i.debug("push: $#" % [symbol]) - if i.scope.hasSymbol(symbol): + let op = i.scope.getSymbol(symbol) + if not op.isNull: i.debug("push: symbol found: $#" % [symbol]) - i.apply i.scope.getSymbol(symbol), symbol + i.apply op, symbol else: # Check if symbol ends with ! (auto-popping) if symbol.len > 1 and symbol[symbol.len-1] == '!': i.debug("push - checking auto-popping symbol: $#" % [symbol]) let apSymbol = symbol[0..symbol.len-2] - if i.scope.hasSymbol(apSymbol): - i.apply i.scope.getSymbol(apSymbol) + let apOp = i.scope.getSymbol(apSymbol) + if not apOp.isNull: + i.apply apOp discard i.pop else: i.debug("push - checking sigil: $#" % [symbol])
M minpkg/core/scope.nimminpkg/core/scope.nim

@@ -20,74 +20,38 @@ elif d.kind == minValOp and d.val.kind == minDictionary:

return d.val proc getSymbolFromPath(scope: ref MinScope, keys: var seq[ - string], acc = 0): MinOperator + string]): MinOperator + +proc isNull*(op: MinOperator): bool = + return op.kind == minValOp and op.val.kind == minNull -proc getSymbol*(scope: ref MinScope, key: string, acc = 0): MinOperator = +proc getSymbol*(scope: ref MinScope, key: string): MinOperator = debug "getSymbol: $#" % [key] if scope.symbols.hasKey(key): return scope.symbols[key] elif key.contains ".": var keys = key.split(".") - return getSymbolFromPath(scope, keys, acc) + return getSymbolFromPath(scope, keys) else: if scope.parent.isNil: - raiseUndefined("Unable to retrieve symbol '$1' (not found)." % key) - return scope.parent.getSymbol(key, acc + 1) + debug("Unable to retrieve symbol '$1' (not found)." % key) + return MinOperator(kind: minValOp, val: MinValue(kind: minNull)) + return scope.parent.getSymbol(key) proc getSymbolFromPath(scope: ref MinScope, keys: var seq[ - string], acc = 0): MinOperator = - let sym = keys[0] - keys.delete(0) - let d = scope.getSymbol(sym, acc) - let dict = d.getDictionary - if not dict.isNil: - if keys.len > 1: - return dict.scope.getSymbolFromPath(keys, acc + 1) - else: - return dict.scope.getSymbol(keys[0], acc + 1) - else: - raiseInvalid("Symbol '$1' is not a dictionary." % sym) - -proc hasSymbolFromPath(scope: ref MinScope, keys: var seq[ - string]): bool - -proc hasSymbol*(scope: ref MinScope, key: string): bool = - debug "hasSymbol: $#" % [key] - if scope.isNil: - return false - else: - #debug "hasSymbol - scope symbols: $#" % [$scope.symbols.keys.toSeq] - if scope.symbols.hasKey(key): - debug "hasSymbol - found $#" % [key] - return true - elif key.contains("."): - var keys = key.split(".") - if keys[0] == "": - raiseInvalid("Symbols cannot start with a dot") - return hasSymbolFromPath(scope, keys) - elif not scope.parent.isNil: - return scope.parent.hasSymbol(key) - else: - return false - -proc hasSymbolFromPath(scope: ref MinScope, keys: var seq[ - string]): bool = + string]): MinOperator = let sym = keys[0] keys.delete(0) - var d: MinOperator - try: - d = scope.getSymbol(sym) - except CatchableError: - return false + let d = scope.getSymbol(sym) let dict = d.getDictionary - debug "hasSymbolFromPath: Found dictionary $# - keys: $#" % [sym, keys.join(".")] if not dict.isNil: if keys.len > 1: - return dict.scope.hasSymbolFromPath(keys) + return dict.scope.getSymbolFromPath(keys) else: - return dict.scope.hasSymbol(keys[0]) + return dict.scope.getSymbol(keys[0]) else: - raiseInvalid("Symbol '$1' is not a dictionary." % sym) + debug("Symbol '$1' is not a dictionary." % sym) + return MinOperator(kind: minValOp, val: MinValue(kind: minNull)) proc delSymbolFromPath(scope: ref MinScope, keys: var seq[ string]): bool
M minpkg/core/utils.nimminpkg/core/utils.nim

@@ -248,6 +248,7 @@ return true

else: let tc = "typeclass:$#" % t let ta = "typealias:$#" % t + let taSym = i.scope.getSymbol(ta) if t.contains(":"): var split = t.split(":") # Typed dictionaries

@@ -255,11 +256,11 @@ if split[0] == "dict":

if value.isTypedDictionary(split[1]): return true return false - elif i.scope.hasSymbol(ta): + elif not taSym.isNull: # Custom type alias - let element = i.scope.getSymbol(ta).val.getString + let element = taSym.val.getString return i.validateValueType(element, value) - elif i.scope.hasSymbol(tc): + elif not i.scope.getSymbol(tc).isNull: # Custom type class var i2 = i.copy(i.filename) i2.withScope():

@@ -292,7 +293,7 @@ const ts = ["bool", "null", "int", "num", "flt", "quot", "dict", "'sym",

"sym", "str", "a"] if ts.contains(s): return true - if i.scope.hasSymbol("typeclass:$#" % s): + if not i.scope.getSymbol("typeclass:$#" % s).isNull: return true for ta in s.split("|"): for to in ta.split("&"):

@@ -302,10 +303,11 @@ return false

if to[0] == '!': tt = to[1..to.len-1] if not ts.contains(tt) and not tt.startsWith("dict:") and - not i.scope.hasSymbol("typeclass:$#" % tt): + i.scope.getSymbol("typeclass:$#" % tt).isNull: let ta = "typealias:$#" % tt - if i.scope.hasSymbol(ta): - return i.validType(i.scope.getSymbol(ta).val.getString) + let taSym = i.scope.getSymbol(ta) + if not taSym.isNull: + return i.validType(taSym.val.getString) return false return true
M minpkg/lib/min_global.nimminpkg/lib/min_global.nim

@@ -419,7 +419,7 @@ i.push q.newVal

def.symbol("defined-symbol?") do (i: In): let vals = i.expect("'sym") - i.push(i.scope.hasSymbol(vals[0].getString).newVal) + i.push((not i.scope.getSymbol(vals[0].getString).isNull).newVal) def.symbol("defined-sigil?") do (i: In): let vals = i.expect("'sym")

@@ -609,8 +609,8 @@

def.symbol("symbol-help") do (i: In): let vals = i.expect("'sym") let s = vals[0].getString - if i.scope.hasSymbol(s): - let sym = i.scope.getSymbol(s) + let sym = i.scope.getSymbol(s) + if not sym.isNull: if not sym.doc.isNil and sym.doc.kind == JObject: var doc = i.fromJson(sym.doc) doc.objType = "help"

@@ -662,9 +662,9 @@ echo ""

for l in lines: echo " " & l echo "===" - if i.scope.hasSymbol(s): + let sym = i.scope.getSymbol(s) + if not sym.isNull: found = true - let sym = i.scope.getSymbol(s) if not sym.doc.isNil and sym.doc.kind == JObject: foundDoc = true displayDoc(sym.doc)