Fix a few parsing bugs at both levels, port some of the method demo over to new_kraken

This commit is contained in:
Nathan Braswell
2020-12-22 02:40:54 -05:00
parent 5152e1d109
commit 8d80f38f76
4 changed files with 46 additions and 9 deletions

View File

@@ -449,7 +449,7 @@ fun read_str(grammar: ref Grammer<KPResult,KPValue>, s: str): pair<int, KPResult
var BSR = fungll(grammar, grammar.start_symbol, s)
var longest = -1
for (var i = 0; i < BSR.data.size; i++;) {
if BSR.data[i].nonterminal == grammar.start_symbol && BSR.data[i].left == 0 && BSR.data[i].idx_into_rule == grammar.nonterminals[(-1*BSR.data[i].nonterminal)-1][BSR.data[i].rule_idx].size {
if BSR.data[i].nonterminal == grammar.start_symbol && BSR.data[i].left == 0 && BSR.data[i].idx_into_rule == grammar.nonterminals[(-1*BSR.data[i].nonterminal)-1][BSR.data[i].rule_idx].size && BSR.data[i].right > longest {
longest = BSR.data[i].right
}
}

View File

@@ -1,5 +1,40 @@
(let (
new_kraken_untyped standard_grammar
)
; {} body translated to do and let
construct_body (rec-lambda (is_do current to_add i)
(if (> (len to_add) i)
(cond (and is_do (= (len (idx to_add i)) 1)) (recurse true (concat current [(idx (idx to_add i) 0)]) to_add (+ i 1))
(= (len (idx to_add i)) 1) (concat current [(recurse true [do (idx (idx to_add i) 0)] to_add (+ i 1))])
true (concat current [(recurse false [let [(idx (idx to_add i) 0) (idx (idx to_add i) 1)] ] to_add (+ i 1))]))
current))
; string interpolation
remove_dollar (rec-lambda (done to_do i j) (cond (>= j (- (len to_do) 2)) (str done (slice to_do i -1))
(= "\\$" (slice to_do j (+ j 2))) (recurse (str done (slice to_do i j) "$") to_do (+ j 2) (+ j 2))
true (recurse done to_do i (+ j 1))))
fixup_str_parts (lambda (s) (remove_dollar "" (slice s 0 -2) 0 0))
new_kraken_untyped (concat standard_grammar (array
(array 'block_member [ 'form ] (lambda (x) [x]))
(array 'block_member [ "let" 'WS * 'atom 'WS * "=" 'WS * 'form ]
(lambda (_ _ name _ _ _ rhs) `(~name ~rhs)))
(array 'form ["{" 'WS * [ 'block_member 'WS * ] * "}"]
(lambda (_ _ inner _) (construct_body true [do] (map (lambda (x) (idx x 0)) inner) 0)))
(array 'form [ "$\"" [ "(#|[%-[]| |[]-~]|(\\\\)|(\\n)|(\\t)|(\\*)|(\\\\$)|
|[ -!]|(\\\\\"))*$" 'form ] * "(#|[%-[]| |[]-~]|(\\\\)|(\\n)|(\\t)|(\\*)|(\\\\$)|
|[ -!]|(\\\\\"))*\"" ]
(lambda (_ string_form_pairs end) `(str ,( flat_map (lambda (x) [ (fixup_str_parts (idx x 0)) (idx x 1) ]) string_form_pairs) ~(fixup_str_parts end))))
(array 'form [ "\\|" 'WS * [ 'atom 'WS * ] * "\\|" 'WS * 'form ]
(lambda (_ _ params _ _ body) `(lambda (,(map (lambda (x) (idx x 0)) params)) ~body)))
)))
(provide new_kraken_untyped)
)

View File

@@ -1,3 +1,5 @@
#lang (with_import "./new_kraken.kp" new_kraken_untyped)
(println "THIS IS ININ the real real finally")
{
let my_var = 1337
(println $"this is string interpolation: $(+ 1 3 4) <- cool right? another $my_var yep even variables")
}

View File

@@ -155,19 +155,19 @@
(insert_into_scope_let
(insert_into_scope_let scope_let_sans_import_gram (quote standard_grammar) (gen_standard_grammar))
(quote with_import) with_import)
(array (read-string (slurp (eval lib_path de))))) root_env))
(array (read-string (slurp (eval lib_path de)) (gen_standard_grammar) (quote start_symbol)))) root_env))
(eval (concat imported_scope_let (array code)) de)))
gen_standard_grammar (vau de () (array
(array (quote WS) (array "( | |
|(;[ -~]*
))+") (lambda (x) nil))
(array (quote atom) (array "-?[0-9]+") (lambda (x) (read-string x)))
(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) (read-string x)))
|[ -!]|(\\\\\"))*\"") (lambda (x) (do (println "read-str for string") (read-string 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 (read-string x))))
true (do (println "read-string for atom") (read-string 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))))