primitives.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 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 |
import tables, strutils, os, osproc, times import parser, interpreter, utils, trex minsym "exit": quit(0) minsym "symbols": var q = newSeq[TMinValue](0) for s in SYMBOLS.keys: q.add s.newVal i.push q.newVal minsym "aliases": var q = newSeq[TMinValue](0) for s in ALIASES: q.add s.newVal i.push q.newVal minsym "debug?": i.push i.debugging.newVal minsym "debug": i.debugging = not i.debugging echo "Debugging: $1" % [$i.debugging] minsym "clear": while i.stack.len > 0: discard i.pop # Common stack operations minsym "id": discard minsym "pop": discard i.pop minsym "dup": i.push i.peek minsym "dip": let q = i.pop if not q.isQuotation: i.error errNoQuotation let v = i.pop for item in q.qVal: i.push item i.push v minsym "swap": let a = i.pop let b = i.pop i.push a i.push b minsym "sip": let a = i.pop let b = i.pop if a.isQuotation and b.isQuotation: i.push b i.push a.qVal i.push b else: i.error(errIncorrect, "Two quotations are required on the stack") # Operations on quotations minsym "quote": let a = i.pop i.push TMinValue(kind: minQuotation, qVal: @[a]) minsym "unquote": let q = i.pop if not q.isQuotation: i.error errNoQuotation for item in q.qVal: i.push item minsym "cons": var q = i.pop let v = i.pop if not q.isQuotation: i.error errNoQuotation q.qVal.add v i.push q minsym "map": let prog = i.pop let list = i.pop if prog.isQuotation and list.isQuotation: i.push newVal(newSeq[TMinValue](0)) for litem in list.qVal: i.push litem for pitem in prog.qVal: i.push pitem i.apply("swap") i.apply("cons") else: i.error(errIncorrect, "Two quotations are required on the stack") minsym "ifte": let fpath = i.pop let tpath = i.pop let check = i.pop if check.isQuotation and tpath.isQuotation and fpath.isQuotation: i.push check.qVal let res = i.pop 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") minsym "while": 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") minsym "filter": let filter = i.pop let list = i.pop var res = newSeq[TMinValue](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") minsym "linrec": 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") # Operations on the whole stack minsym "dump": echo i.dump minsym "stack": var s = i.stack i.push s # Operations on quotations or strings minsym "concat": 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") minsym "first": 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") minsym "rest": 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") # Operations on strings minsym "split": let sep = i.pop let s = i.pop if s.isString and sep.isString: for e in s.strVal.split(sep.strVal): i.push e.newVal else: i.error errIncorrect, "Two strings are required on the stack" # Arithmetic minsym "+": let a = i.pop let b = i.pop if a.isInt: if b.isInt: i.push newVal(a.intVal + b.intVal) elif b.isFloat: i.push newVal(a.intVal.float + b.floatVal) else: i.error(errTwoNumbersRequired) elif a.isFloat: if b.isFloat: i.push newVal(a.floatVal + b.floatVal) elif b.isInt: i.push newVal(a.floatVal + b.intVal.float) else: i.error(errTwoNumbersRequired) minsym "-": let a = i.pop let b = i.pop if a.isInt: if b.isInt: i.push newVal(b.intVal - a.intVal) elif b.isFloat: i.push newVal(b.floatVal - a.intVal.float) else: i.error(errTwoNumbersRequired) elif a.isFloat: if b.isFloat: i.push newVal(b.floatVal - a.floatVal) elif b.isInt: i.push newVal(b.intVal.float - a.floatVal) else: i.error(errTwoNumbersRequired) minsym "*": let a = i.pop let b = i.pop if a.isInt: if b.isInt: i.push newVal(a.intVal * b.intVal) elif b.isFloat: i.push newVal(a.intVal.float * b.floatVal) else: i.error(errTwoNumbersRequired) elif a.isFloat: if b.isFloat: i.push newVal(a.floatVal * b.floatVal) elif b.isInt: i.push newVal(a.floatVal * b.intVal.float) else: i.error(errTwoNumbersRequired) minsym "/": let a = i.pop let b = i.pop if b.isInt and b.intVal == 0: i.error errDivisionByZero if a.isInt: if b.isInt: i.push newVal(b.intVal / a.intVal) elif b.isFloat: i.push newVal(b.floatVal / a.intVal.float) else: i.error(errTwoNumbersRequired) elif a.isFloat: if b.isFloat: i.push newVal(b.floatVal / a.floatVal) elif b.isInt: i.push newVal(b.intVal.float / a.floatVal) else: i.error(errTwoNumbersRequired) minsym "div": let b = i.pop let a = i.pop if a.isInt and b.isInt: i.push(newVal(a.intVal div b.intVal)) else: i.error errIncorrect, "Two integers are required on the stack" minsym "mod": let b = i.pop let a = i.pop if a.isInt and b.isInt: i.push(newVal(a.intVal mod b.intVal)) else: i.error errIncorrect, "Two integers are required on the stack" # Language constructs minsym "def": let q1 = i.pop let q2 = i.pop if q1.isQuotation and q2.isQuotation: if q1.qVal.len == 1 and q1.qVal[0].kind == minSymbol: minsym q1.qVal[0].symVal: i.evaluating = true echo q2 i.push q2.qVal i.evaluating = false else: i.error errIncorrect, "The top quotation must contain only one symbol value" else: i.error errIncorrect, "Two quotations are required on the stack" minsym "eval": let s = i.pop if s.isString: i.eval s.strVal else: i.error(errIncorrect, "A string is required on the stack") minsym "load": let s = i.pop if s.isString: i.load s.strVal else: i.error(errIncorrect, "A string is required on the stack") # Comparison operators minsym ">": let n2 = i.pop let n1 = i.pop if n1.isNumber and n2.isNumber: if n1.isInt and n2.isInt: i.push newVal(n1.intVal > n2.intVal) elif n1.isInt and n2.isFloat: i.push newVal(n1.intVal.float > n2.floatVal) elif n1.isFloat and n2.isFloat: i.push newVal(n1.floatVal > n2.floatVal) elif n1.isFloat and n2.isInt: i.push newVal(n1.floatVal > n2.intVal.float) elif n1.isString and n2.isString: i.push newVal(n1.strVal > n2.strVal) else: i.error(errIncorrect, "Two numbers or two strings are required on the stack") minsym ">=": let n2 = i.pop let n1 = i.pop if n1.isNumber and n2.isNumber: if n1.isInt and n2.isInt: i.push newVal(n1.intVal >= n2.intVal) elif n1.isInt and n2.isFloat: i.push newVal(n1.intVal.float >= n2.floatVal) elif n1.isFloat and n2.isFloat: i.push newVal(n1.floatVal >= n2.floatVal) elif n1.isFloat and n2.isInt: i.push newVal(n1.floatVal >= n2.intVal.float) elif n1.isString and n2.isString: i.push newVal(n1.strVal >= n2.strVal) else: i.error(errIncorrect, "Two numbers or two strings are required on the stack") minsym "<": let n1 = i.pop let n2 = i.pop if n1.isNumber and n2.isNumber: if n1.isInt and n2.isInt: i.push newVal(n1.intVal > n2.intVal) elif n1.isInt and n2.isFloat: i.push newVal(n1.intVal.float > n2.floatVal) elif n1.isFloat and n2.isFloat: i.push newVal(n1.floatVal > n2.floatVal) elif n1.isFloat and n2.isInt: i.push newVal(n1.floatVal > n2.intVal.float) elif n1.isString and n2.isString: i.push newVal(n1.strVal > n2.strVal) else: i.error(errIncorrect, "Two numbers or two strings are required on the stack") minsym "<=": let n1 = i.pop let n2 = i.pop if n1.isNumber and n2.isNumber: if n1.isInt and n2.isInt: i.push newVal(n1.intVal >= n2.intVal) elif n1.isInt and n2.isFloat: i.push newVal(n1.intVal.float >= n2.floatVal) elif n1.isFloat and n2.isFloat: i.push newVal(n1.floatVal >= n2.floatVal) elif n1.isFloat and n2.isInt: i.push newVal(n1.floatVal >= n2.intVal.float) elif n1.isString and n2.isString: i.push newVal(n1.strVal >= n2.strVal) else: i.error(errIncorrect, "Two numbers or two strings are required on the stack") minsym "==": let n2 = i.pop let n1 = i.pop if (n1.kind == n2.kind or (n1.isNumber and n2.isNumber)) and not n1.isSymbol: i.push newVal(n1 == n2) else: i.error(errIncorrect, "Two non-symbol values of similar type are required") minsym "!=": let n2 = i.pop let n1 = i.pop if (n1.kind == n2.kind or (n1.isNumber and n2.isNumber)) and not n1.isSymbol: i.push newVal(not (n1 == n2)) else: i.error(errIncorrect, "Two non-symbol values of similar type are required") # Boolean Logic minsym "not": let b = i.pop if b.isBool: i.push newVal(not b.boolVal) else: i.error(errIncorrect, "A bool value is required on the stack") minsym "and": let a = i.pop let b = i.pop if a.isBool and b.isBool: i.push newVal(a.boolVal and b.boolVal) else: i.error(errIncorrect, "Two bool values are required on the stack") minsym "or": let a = i.pop let b = i.pop if a.isBool and b.isBool: i.push newVal(a.boolVal or b.boolVal) else: i.error(errIncorrect, "Two bool values are required on the stack") minsym "xor": let a = i.pop let b = i.pop if a.isBool and b.isBool: i.push newVal(a.boolVal xor b.boolVal) else: i.error(errIncorrect, "Two bool values are required on the stack") # Time minsym "timestamp": i.push getTime().int.newVal minsym "now": i.push epochTime().newVal # 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 "cd": let f = i.pop if f.isString: try: f.strVal.setCurrentDir except: warn getCurrentExceptionMsg() else: i.error errIncorrect, "A string is required on the stack" 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") minsym "system": let a = i.pop if a.isString: i.push execShellCmd(a.strVal).newVal else: i.error(errIncorrect, "A string is required on the stack") minsym "run": let a = i.pop if a.isString: let words = a.strVal.split(" ") let cmd = words[0] var args = newSeq[string](0) if words.len > 1: args = words[1..words.len-1] i.push execProcess(cmd, args, nil, {poUsePath}).newVal else: i.error(errIncorrect, "A string is required on the stack") minsym "getenv": let a = i.pop if a.isString: i.push a.strVal.getEnv.newVal else: i.error(errIncorrect, "A string is required on the stack") minsym "putenv": let value = i.pop let key = i.pop if value.isString and key.isString: key.strVal.putEnv value.strVal else: i.error(errIncorrect, "Two strings are required on the stack") minsym "os": i.push hostOS.newVal minsym "cpu": i.push hostCPU.newVal minsym "file?": let f = i.pop if f.isString: i.push f.strVal.fileExists.newVal else: i.error errIncorrect, "A string is required on the stack" minsym "dir?": let f = i.pop if f.isString: i.push f.strVal.dirExists.newVal else: i.error errIncorrect, "A string is required on the stack" minsym "rm": let f = i.pop if f.isString: try: f.strVal.removeFile except: warn getCurrentExceptionMsg() else: i.error errIncorrect, "A string is required on the stack" minsym "cp": let b = i.pop let a = i.pop if a.isString and b.isString: try: copyFile a.strVal, b.strVal except: warn getCurrentExceptionMsg() else: i.error errIncorrect, "Two strings are required on the stack" minsym "mv": let b = i.pop let a = i.pop if a.isString and b.isString: try: moveFile a.strVal, b.strVal except: warn getCurrentExceptionMsg() else: i.error errIncorrect, "Two strings are required on the stack" minsym "rmdir": let f = i.pop if f.isString: try: f.strVal.removeDir except: warn getCurrentExceptionMsg() else: i.error errIncorrect, "A string is required on the stack" minsym "mkdir": let f = i.pop if f.isString: try: f.strVal.createDir except: warn getCurrentExceptionMsg() else: i.error errIncorrect, "A string is required on the stack" # Aliases minalias "quit", "exit" minalias "&", "concat" minalias "cat", "concat" minalias "%", "print" minalias ":", "def" minalias "eq", "==" minalias "noteq", "!=" minalias "gt", ">" minalias "lt", "<" minalias "gte", ">=" minalias "lte", "<=" minalias "echo", "puts" minalias "shell", "system" minalias "sh", "system" minalias "!", "system" minalias "!&", "run" minalias "$", "getenv" minalias "$=", "putenv" minalias "zap", "pop" minalias "unit", "quote" minalias "i", "unquote" minalias "i", "apply" minalias "select", "filter" minalias "empty", "clear" |