Fix some longstanding string bugs and stop cheating with read-string in standard_grammer, implementing both unescape and string-to-int in k'

This commit is contained in:
Nathan Braswell
2021-01-04 00:11:15 -05:00
parent 6c0a46099a
commit ddd5ce7032
2 changed files with 53 additions and 8 deletions

View File

@@ -149,6 +149,33 @@
print_through
)
insert_into_scope_let (lambda (scope_let name item) (array (idx scope_let 0) (concat (idx scope_let 1) (array name (array quote item)))))
string-to-int (lambda (s) (let (
helper (rec-lambda recurse (s i result)
(if (< i (len s))
(recurse s (+ i 1) (+ (* 10 result) (- (idx s i) (idx "0" 0))))
result
)
))
(if (= (idx s 0) (idx "-" 0))
(- (helper s 1 0))
(helper s 0 0)
)))
unescape-str (lambda (s) (let (
helper (rec-lambda recurse (s i r)
(cond (>= (+ 1 i) (len s)) r
(= (idx s i) (idx "\\" 0)) (cond (= (+ i 1) (len s)) "BAD ESCAPE AT END"
(= (idx s (+ i 1)) (idx "n" 0)) (recurse s (+ i 2) (str r "\n"))
(= (idx s (+ i 1)) (idx "t" 0)) (recurse s (+ i 2) (str r "\t"))
(= (idx s (+ i 1)) (idx "0" 0)) (recurse s (+ i 2) (str r "\0"))
(= (idx s (+ i 1)) (idx "\\" 0)) (recurse s (+ i 2) (str r "\\"))
(= (idx s (+ i 1)) (idx "\"" 0)) (recurse s (+ i 2) (str r "\""))
true "BAD ESCAPE IS NORMAL CHAR"
)
true (recurse s (+ i 1) (str r (slice s i (+ i 1))))
)
)) (helper s 1 "")))
scope_let (let-vrec (
with_import (vau de (lib_path code)
(let (imported_scope_let (eval (concat
@@ -161,13 +188,13 @@
(array (quote WS) (array "( | |
|(;[ -~]*
))+") (lambda (x) nil))
(array (quote atom) (array "-?[0-9]+") (lambda (x) (do (println "read-str for number") (read-string x))))
(array (quote atom) (array "\"([#-[]| |[]-~]|(\\\\)|(\\n)|(\\t)|(\\\\*)|(\\0)|
|[ -!]|(\\\\\"))*\"") (lambda (x) (do (println "read-str for string") (read-string x))))
(array (quote atom) (array "-?[0-9]+") (lambda (x) (string-to-int x)))
(array (quote atom) (array "\"([#-[]| |[]-~]|(\\\\\\\\)|(\\\\n)|(\\\\t)|(\\*)|(\\\\0)|
|[ -!]|(\\\\\"))*\"") (lambda (x) (unescape-str x)))
(array (quote atom) (array "-|(([a-z]|[A-Z]|_|\\*|/|\\?|\\+|!|=|&|<|>|%)([a-z]|[A-Z]|_|[0-9]|\\*|\\?|\\+|-|!|=|&|<|>|%)*)") (lambda (x) (cond (= "true" x) true
(= "false" x) false
(= "nil" x) nil
true (do (println "read-string for atom") (read-string x)))))
true (str-to-symbol x))))
(array (quote form) (array (quote atom)) (lambda (x) x))
(array (quote form) (array "\\(" (quote WS) * "\\)" ) (lambda (_ _ _) (array)))
(array (quote form) (array "\\(" (quote WS) * (quote form) (array (quote WS) + (quote form)) * (quote WS) * "\\)" ) (lambda (_ _ head tail _ _) (concat (array head) (map (lambda (x) (idx x 1)) tail))))