lib/lang.nim
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
import critbits, strutils import ../core/types, ../core/parser, ../core/interpreter, ../core/utils ROOT .symbol("exit") do (i: In): quit(0) .symbol("symbols") do (i: In): var q = newSeq[MinValue](0) var scope = i.scope.parent while not scope.isNil: for s in scope.symbols.keys: q.add s.newVal scope = scope.parent i.push q.newVal .symbol("sigils") do (i: In): var q = newSeq[MinValue](0) for s in i.scope.parent.sigils.keys: q.add s.newVal i.push q.newVal .symbol("debug?") do (i: In): i.push i.debugging.newVal .symbol("debug") do (i: In): i.debugging = not i.debugging echo "Debugging: $1" % [$i.debugging] # Language constructs .symbol("let") do (i: In): var q2 = i.pop # new (can be a quoted symbol or a string) var q1 = i.pop # existing (auto-quoted) var symbol: string if not q1.isQuotation: q1 = @[q1].newVal if q2.isString: symbol = q2.strVal elif q2.isQuotation and q2.qVal.len == 1 and q2.qVal[0].kind == minSymbol: symbol = q2.qVal[0].symVal else: i.error errIncorrect, "The top quotation must contain only one symbol value" i.debug "[let] " & symbol & " = " & $q1 i.scope.parent.symbols[symbol] = proc(i: var MinInterpreter) = i.evaluating = true i.push q1.qVal i.evaluating = false .symbol("bind") do (i: In): var q2 = i.pop # new (can be a quoted symbol or a string) var q1 = i.pop # existing (auto-quoted) var symbol: string if not q1.isQuotation: q1 = @[q1].newVal if q2.isString: symbol = q2.strVal elif q2.isQuotation and q2.qVal.len == 1 and q2.qVal[0].kind == minSymbol: symbol = q2.qVal[0].symVal else: i.error errIncorrect, "The top quotation must contain only one symbol value" i.debug "[bind] " & symbol & " = " & $q1 i.scope.setSymbol(symbol) do (i: In): i.evaluating = true i.push q1.qVal i.evaluating = false .symbol("module") do (i: In): let name = i.pop var code = i.pop if not name.isString or not code.isQuotation: i.error(errIncorrect, "A string and a quotation are require on the stack") let id = name.strVal let scope = i.scope let stack = i.copystack i.newScope(id, code): #<-- for item in code.qVal: i.push item let p = proc(i: In) = i.evaluating = true i.push code i.evaluating = false i.scope.parent.symbols[id] = p i.stack = stack .symbol("import") do (i: In): var mdl: MinValue try: i.scope.getSymbol(i.pop.strVal)(i) mdl = i.pop except: echo getCurrentExceptionMsg() if not mdl.isQuotation: i.error errNoQuotation if not mdl.scope.isNil: #echo "MODULE SCOPE PARENT: ", mdl.scope.name for sym, val in mdl.scope.symbols.pairs: i.debug "[$1 - import] $2:$3" % [i.scope.parent.name, i.scope.name, sym] i.scope.parent.symbols[sym] = val .sigil("'") do (i: In): i.push(@[MinValue(kind: minSymbol, symVal: i.pop.strVal)].newVal) .symbol("sigil") do (i: In): var q1 = i.pop let q2 = i.pop if q1.isString: q1 = @[q1].newVal if q1.isQuotation and q2.isQuotation: if q1.qVal.len == 1 and q1.qVal[0].kind == minSymbol: var symbol = q1.qVal[0].symVal if symbol.len == 1: if i.scope.parent.sigils.hasKey(symbol): i.error errSystem, "Sigil '$1' already exists" % [symbol] i.scope.parent.sigils[symbol] = proc(i: var MinInterpreter) = i.evaluating = true i.push q2.qVal i.evaluating = false else: i.error errIncorrect, "A sigil can only have one character" else: i.error errIncorrect, "The top quotation must contain only one symbol value" else: i.error errIncorrect, "Two quotations are required on the stack" .symbol("eval") do (i: In): let s = i.pop if s.isString: i.eval s.strVal else: i.error(errIncorrect, "A string is required on the stack") .symbol("load") do (i: In): let s = i.pop if s.isString: i.load s.strVal else: i.error(errIncorrect, "A string is required on the stack") # Operations on the whole stack .symbol("clear") do (i: In): while i.stack.len > 0: discard i.pop .symbol("dump") do (i: In): echo i.dump .symbol("stack") do (i: In): var s = i.stack i.push s # Operations on quotations or strings .symbol("concat") do (i: In): var q1 = i.pop var q2 = i.pop if q1.isString and q2.isString: let s = q2.strVal & q1.strVal i.push newVal(s) elif q1.isQuotation and q2.isQuotation: let q = q2.qVal & q1.qVal i.push newVal(q) else: i.error(errIncorrect, "Two quotations or two strings are required on the stack") .symbol("first") do (i: In): var q = i.pop if q.isQuotation: i.push q.qVal[0] elif q.isString: i.push newVal($q.strVal[0]) else: i.error(errIncorrect, "A quotation or a string is required on the stack") .symbol("rest") do (i: In): var q = i.pop if q.isQuotation: i.push newVal(q.qVal[1..q.qVal.len-1]) elif q.isString: i.push newVal(q.strVal[1..q.strVal.len-1]) else: i.error(errIncorrect, "A quotation or a string is required on the stack") .symbol("quote") do (i: In): let a = i.pop i.push MinValue(kind: minQuotation, qVal: @[a]) .symbol("unquote") do (i: In): var q = i.pop if not q.isQuotation: i.error errNoQuotation i.newScope("<unquote-push>", q): for item in q.qVal: i.push item .symbol("append") do (i: In): var q = i.pop let v = i.pop if not q.isQuotation: i.error errNoQuotation q.qVal.add v i.push q .symbol("cons") do (i: In): var q = i.pop let v = i.pop if not q.isQuotation: i.error errNoQuotation q.qVal = @[v] & q.qVal i.push q .symbol("at") do (i: In): var index = i.pop var q = i.pop if index.isInt and q.isQuotation: i.push q.qVal[index.intVal] else: i.error errIncorrect, "An integer and a quotation are required on the stack" .symbol("map") do (i: In): let prog = i.pop let list = i.pop if prog.isQuotation and list.isQuotation: i.push newVal(newSeq[MinValue](0)) for litem in list.qVal: i.push litem for pitem in prog.qVal: i.push pitem i.apply("swap") i.apply("append") else: i.error(errIncorrect, "Two quotations are required on the stack") .symbol("times") do (i: In): let t = i.pop let prog = i.pop if t.isInt and prog.isQuotation: for c in 1..t.intVal: for pitem in prog.qVal: i.push pitem else: i.error errIncorrect, "An integer and a quotation are required on the stack" .symbol("ifte") do (i: In): let fpath = i.pop let tpath = i.pop let check = i.pop var stack = i.copystack if check.isQuotation and tpath.isQuotation and fpath.isQuotation: i.push check.qVal let res = i.pop i.stack = stack if res.isBool and res.boolVal == true: i.push tpath.qVal else: i.push fpath.qVal else: i.error(errIncorrect, "Three quotations are required on the stack") # TODO test (add new scope?) .symbol("while") do (i: In): let d = i.pop let b = i.pop if b.isQuotation and d.isQuotation: i.push b.qVal var check = i.pop while check.isBool and check.boolVal == true: i.push d.qVal i.push b.qVal check = i.pop else: i.error(errIncorrect, "Two quotations are required on the stack") # TODO test (add new scope?) .symbol("filter") do (i: In): let filter = i.pop let list = i.pop var res = newSeq[MinValue](0) if filter.isQuotation and list.isQuotation: for e in list.qVal: i.push e i.push filter.qVal var check = i.pop if check.isBool and check.boolVal == true: res.add e i.push res.newVal else: i.error(errIncorrect, "Two quotations are required on the stack") .symbol("linrec") do (i: In): var r2 = i.pop var r1 = i.pop var t = i.pop var p = i.pop if p.isQuotation and t.isQuotation and r1.isQuotation and r2.isQuotation: i.linrec(p, t, r1, r2) else: i.error(errIncorrect, "Four quotations are required on the stack") .finalize() |