New kraken with import and #lang passed start symbol

This commit is contained in:
Nathan Braswell
2021-01-02 13:55:07 -05:00
parent 4ed9af307f
commit 6c0a46099a
4 changed files with 53 additions and 43 deletions

1
import_test.kp Normal file
View File

@@ -0,0 +1 @@
(let (a 123) (provide a))

View File

@@ -1,7 +1,5 @@
(let ( (let (
; First quick lookup function, since maps are not built in ; First quick lookup function, since maps are not built in
get-value-helper (rec-lambda recurse (dict key i) (if (>= i (len dict)) get-value-helper (rec-lambda recurse (dict key i) (if (>= i (len dict))
nil nil
@@ -29,9 +27,11 @@
(if (> (len 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)) (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))]) (= (len (idx to_add i)) 1) (concat current [(recurse true [do (idx (idx to_add i) 0)] to_add (+ i 1))])
(= (len (idx to_add i)) 3) (concat current [[with_import (idx (idx to_add i) 0) (recurse false [do] 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))])) true (concat current [(recurse false [let [(idx (idx to_add i) 0) (idx (idx to_add i) 1)] ] to_add (+ i 1))]))
current)) current))
; string interpolation ; string interpolation
remove_dollar (rec-lambda recurse (done to_do i j) (cond (>= j (- (len to_do) 2)) (str done (slice to_do i -1)) remove_dollar (rec-lambda recurse (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)) (= "\\$" (slice to_do j (+ j 2))) (recurse (str done (slice to_do i j) "$") to_do (+ j 2) (+ j 2))
@@ -63,18 +63,24 @@
(array 'block_member [ "fun" 'WS 'atom 'WS * "\\(" 'call_innards "\\)" 'WS * 'form ] (array 'block_member [ "fun" 'WS 'atom 'WS * "\\(" 'call_innards "\\)" 'WS * 'form ]
(lambda (_ _ name _ _ params _ _ body) `(~name (~lambda (,params) ~body)))) (lambda (_ _ name _ _ params _ _ body) `(~name (~lambda (,params) ~body))))
(array 'block_member [ 'form ] (lambda (x) [x]))
(array 'block_member [ "let" 'WS * 'atom 'WS * "=" 'WS * 'form ]
(lambda (_ _ name _ _ _ rhs) `(~name ~rhs)))
; object syntax ; object syntax
(array 'block_member ["obj" 'WS 'atom "\\(" ['WS * 'atom] * 'WS * "\\)" 'WS * "{" 'WS * ['atom 'WS * 'form 'WS *] * "}"] (array 'block_member ["obj" 'WS 'atom "\\(" ['WS * 'atom] * 'WS * "\\)" 'WS * "{" 'WS * ['atom 'WS * 'form 'WS *] * "}"]
(lambda (_ _ name _ members _ _ _ _ _ methods _) (lambda (_ _ name _ members _ _ _ _ _ methods _)
[name (make_constructor name (map (lambda (x) (idx x 1)) members) [name (make_constructor name (map (lambda (x) (idx x 1)) members)
(map (lambda (x) [(idx x 0) (idx x 2)]) methods))])) (map (lambda (x) [(idx x 0) (idx x 2)]) methods))]))
; import
(array 'block_member [ "with_import" 'WS 'atom 'WS * ":" ]
(lambda (_ _ file _ _) [file 0 0]))
(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 ] * "}"] (array 'form ["{" 'WS * [ 'block_member 'WS ] * "}"]
(lambda (_ _ inner _) (construct_body true [do] (map (lambda (x) (idx x 0)) inner) 0))) (lambda (_ _ inner _) (construct_body true [do] (map (lambda (x) (idx x 0)) inner) 0)))
(array 'new_kraken_start_symbol [ 'WS * [ 'block_member 'WS ] * ]
(lambda (_ inner) (construct_body true [do] (map (lambda (x) (idx x 0)) inner) 0)))
(array 'form [ "$\"" [ "(#|[%-[]| |[]-~]|(\\\\)|(\\n)|(\\t)|(\\*)|(\\\\$)| (array 'form [ "$\"" [ "(#|[%-[]| |[]-~]|(\\\\)|(\\n)|(\\t)|(\\*)|(\\\\$)|
|[ -!]|(\\\\\"))*$" 'form ] * "(#|[%-[]| |[]-~]|(\\\\)|(\\n)|(\\t)|(\\*)|(\\\\$)| |[ -!]|(\\\\\"))*$" 'form ] * "(#|[%-[]| |[]-~]|(\\\\)|(\\n)|(\\t)|(\\*)|(\\\\$)|

View File

@@ -1,38 +1,41 @@
#lang (with_import "./new_kraken.kp" new_kraken_untyped) #lang (with_import "./new_kraken.kp" new_kraken_untyped) new_kraken_start_symbol
{
let my_var = 1337
(println $"this is string interpolation: $(+ 1 3 4) <- cool right? another $my_var yep even variables")
obj Point( x y ) { let my_var = 1337
add |self other| { Point((+ self.x other.x) (+ self.y other.y)) } (println $"this is string interpolation: $(+ 1 3 4) <- cool right? another $my_var yep even variables")
sub |self other| { Point((- self.x other.x) (- self.y other.y)) }
to_str |self| { str("x: " self.x ", y: " self.y) }
}
fun say_hi(name) { obj Point( x y ) {
println("hayo" name) add |self other| { Point((+ self.x other.x) (+ self.y other.y)) }
} sub |self other| { Point((- self.x other.x) (- self.y other.y)) }
to_str |self| { str("x: " self.x ", y: " self.y) }
fun test() {
let plus_1 = |x| (+ x 1)
let a = 1
let b = plus_1(a)
println("some" b)
say_hi("Marcus")
let p1 = Point(1 2)
let p2 = Point(3 4)
let p3 = p1.add(p2)
let p4 = p1.sub(p2)
say_hi("Charlie/Betty")
println("p1:" p1.to_str)
println("p2:" p2.to_str)
println("p3:" p3.to_str)
println("p4:" p4.to_str)
(+ a b)
}
println("Test result is" test())
} }
fun say_hi(name) {
println("hayo" name)
}
fun test() {
let plus_1 = |x| (+ x 1)
let a = 1
let b = plus_1(a)
println("some" b)
say_hi("Marcus")
let p1 = Point(1 2)
let p2 = Point(3 4)
let p3 = p1.add(p2)
let p4 = p1.sub(p2)
say_hi("Charlie/Betty")
println("p1:" p1.to_str)
println("p2:" p2.to_str)
println("p3:" p3.to_str)
println("p4:" p4.to_str)
println("before + a b" (+ a b))
(with_import "./import_test.kp" (println "after + a b" (+ a b)))
println("post after + a b" (+ a b))
with_import "./import_test.kp":
println("post new impot after + a b" (+ a b))
}
println("Test result is" test())

View File

@@ -179,15 +179,15 @@
(array (quote form) (array "~" (quote WS) * (quote form)) (lambda (_ _ x) (array (quote unquote) x))) (array (quote form) (array "~" (quote WS) * (quote form)) (lambda (_ _ x) (array (quote unquote) x)))
(array (quote form) (array "," (quote WS) * (quote form)) (lambda (_ _ x) (array (quote splice-unquote) x))) (array (quote form) (array "," (quote WS) * (quote form)) (lambda (_ _ x) (array (quote splice-unquote) x)))
(array (quote start_symbol) (array (quote WS) * (quote form) (quote WS) *) (lambda (_ f _) f)) (array (quote start_symbol) (array (quote WS) * (quote form) (quote WS) *) (lambda (_ f _) f))
(array (quote start_symbol) (array (quote WS) * "#lang" (quote WS) * (quote form) "([ -~]| (array (quote start_symbol) (array (quote WS) * "#lang" (quote WS) (quote form) (quote WS) (quote form) "([ -~]|
)*") )*")
(lambda (_ _ _ gram source) (do (println "gonna do that # yo") (read-string source (lambda (_ _ _ gram _ symbol source) (do (println "gonna do that # yo") (read-string source
(eval (concat (eval (concat
(insert_into_scope_let (insert_into_scope_let
(insert_into_scope_let scope_let_sans_import_gram (quote standard_grammar) (gen_standard_grammar)) (insert_into_scope_let scope_let_sans_import_gram (quote standard_grammar) (gen_standard_grammar))
(quote with_import) with_import) (quote with_import) with_import)
(array gram)) root_env) (array gram)) root_env)
(quote start_symbol))))) symbol))))
)) ))
) )
(insert_into_scope_let (insert_into_scope_let