More object syntax
This commit is contained in:
35
k_prime.krak
35
k_prime.krak
@@ -1237,12 +1237,7 @@ fun main(argc: int, argv: **char): int {
|
|||||||
})))
|
})))
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
env->set(str("add_grammar_rule"), make_builtin_function(str("add_grammar_rule"), fun(params: vec<MalValue>): MalResult {
|
var add_grammer_rule_helper: fun(str, vec<MalValue>, MalValue, fun(ref MalValue, ref vec<MalResult>): MalResult): MalResult = fun(nonterminal_str: str, rule: vec<MalValue>, data: MalValue, f: fun(ref MalValue, ref vec<MalResult>): MalResult): MalResult {
|
||||||
if params.size != 3 || !params[0].is_symbol() || !params[1].is_vector() {
|
|
||||||
return MalResult::Err(malString(str("add_grammar_rule called with wrong number or type of params")))
|
|
||||||
} else {
|
|
||||||
var nonterminal_str = params[0].get_symbol_text()
|
|
||||||
var rule = params[1].get_vector_rc().get()
|
|
||||||
var int_rule = vec<int>()
|
var int_rule = vec<int>()
|
||||||
for (var i = 0; i < rule.size; i++;) {
|
for (var i = 0; i < rule.size; i++;) {
|
||||||
if rule[i].is_int() {
|
if rule[i].is_int() {
|
||||||
@@ -1258,6 +1253,23 @@ fun main(argc: int, argv: **char): int {
|
|||||||
int_rule.add(grammar.add_terminal(rule[i].get_string(), malNil(), fun(f: ref MalValue, input: ref str, l: int, r: int): MalResult {
|
int_rule.add(grammar.add_terminal(rule[i].get_string(), malNil(), fun(f: ref MalValue, input: ref str, l: int, r: int): MalResult {
|
||||||
return MalResult::Ok(malString(input.slice(l,r)))
|
return MalResult::Ok(malString(input.slice(l,r)))
|
||||||
}))
|
}))
|
||||||
|
} else if rule[i].is_vector() {
|
||||||
|
// A sequence!
|
||||||
|
var sub_rule_names = nonterminal_str + "_seq_" + new_tmp()
|
||||||
|
var inner_rule = add_grammer_rule_helper(sub_rule_names, rule[i].get_vector_rc().get(), malNil(), fun(_: ref MalValue, seq: ref vec<MalResult>): MalResult {
|
||||||
|
var to_ret = vec<MalValue>()
|
||||||
|
for (var i = 0; i < seq.size; i++;) {
|
||||||
|
if is_err(seq[i]) {
|
||||||
|
return seq[i]
|
||||||
|
}
|
||||||
|
to_ret.add(get_value(seq[i]))
|
||||||
|
}
|
||||||
|
return MalResult::Ok(malVector(to_ret))
|
||||||
|
})
|
||||||
|
if is_err(inner_rule) {
|
||||||
|
return inner_rule
|
||||||
|
}
|
||||||
|
int_rule.add(get_value(inner_rule).get_int())
|
||||||
} else {
|
} else {
|
||||||
match (rule[i].internal) {
|
match (rule[i].internal) {
|
||||||
MalValue_int::BuiltinFunction(f) {
|
MalValue_int::BuiltinFunction(f) {
|
||||||
@@ -1293,7 +1305,15 @@ fun main(argc: int, argv: **char): int {
|
|||||||
return MalResult::Err(malString(str("add_grammar_rule called with not symbol, int, or string in rule")))
|
return MalResult::Err(malString(str("add_grammar_rule called with not symbol, int, or string in rule")))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
grammar.add_to_or_create_nonterminal(nonterminal_str, int_rule, params[2], fun(f: ref MalValue, x: ref vec<MalResult>): MalResult {
|
return MalResult::Ok(malInt(grammar.add_to_or_create_nonterminal(nonterminal_str, int_rule, data, f)))
|
||||||
|
}
|
||||||
|
env->set(str("add_grammar_rule"), make_builtin_function(str("add_grammar_rule"), fun(params: vec<MalValue>): MalResult {
|
||||||
|
if params.size != 3 || !params[0].is_symbol() || !params[1].is_vector() {
|
||||||
|
return MalResult::Err(malString(str("add_grammar_rule called with wrong number or type of params")))
|
||||||
|
} else {
|
||||||
|
var nonterminal_str = params[0].get_symbol_text()
|
||||||
|
var rule = params[1].get_vector_rc().get()
|
||||||
|
return add_grammer_rule_helper(nonterminal_str, rule, params[2], fun(f: ref MalValue, x: ref vec<MalResult>): MalResult {
|
||||||
var params = vec<MalValue>()
|
var params = vec<MalValue>()
|
||||||
for (var i = 0; i < x.size; i++;) {
|
for (var i = 0; i < x.size; i++;) {
|
||||||
if is_err(x[i]) {
|
if is_err(x[i]) {
|
||||||
@@ -1303,7 +1323,6 @@ fun main(argc: int, argv: **char): int {
|
|||||||
}
|
}
|
||||||
return function_call(f, params)
|
return function_call(f, params)
|
||||||
})
|
})
|
||||||
return MalResult::Ok(malNil())
|
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
var ERS = fun(params: vec<MalValue>): MalResult {
|
var ERS = fun(params: vec<MalValue>): MalResult {
|
||||||
|
|||||||
57
method.kp
57
method.kp
@@ -1,6 +1,4 @@
|
|||||||
|
|
||||||
(def! our_obj (with-meta [0] (fn* () (set-nth! our_obj 0 (+ 1 (nth our_obj 0))))))
|
|
||||||
|
|
||||||
(def! get-value-helper (fn* (dict key idx) (if (>= idx (count dict)) nil (if (= key (nth dict idx)) (nth dict (+ idx 1)) (get-value-helper dict key (+ idx 2))))))
|
(def! get-value-helper (fn* (dict key idx) (if (>= idx (count dict)) nil (if (= key (nth dict idx)) (nth dict (+ idx 1)) (get-value-helper dict key (+ idx 2))))))
|
||||||
(def! get-value (fn* (dict key) (get-value-helper dict key 0)))
|
(def! get-value (fn* (dict key) (get-value-helper dict key 0)))
|
||||||
(def! method-call (fn* (object method & arguments) (let* (method_fn (get-value (meta object) method))
|
(def! method-call (fn* (object method & arguments) (let* (method_fn (get-value (meta object) method))
|
||||||
@@ -9,33 +7,31 @@
|
|||||||
(apply method_fn object arguments)))))
|
(apply method_fn object arguments)))))
|
||||||
; method call syntax
|
; method call syntax
|
||||||
(add_grammar_rule 'form [ 'form "\\." 'atom 'optional_WS "\\(" 'optional_WS 'space_forms 'optional_WS "\\)" ] (fn* (o _ m _ _ _ p _ _) `(method-call ~o '~m ,p)))
|
(add_grammar_rule 'form [ 'form "\\." 'atom 'optional_WS "\\(" 'optional_WS 'space_forms 'optional_WS "\\)" ] (fn* (o _ m _ _ _ p _ _) `(method-call ~o '~m ,p)))
|
||||||
(def! actual_obj (with-meta [0] [
|
; object syntax
|
||||||
'inc (fn* (o) (set-nth! o 0 (+ (nth o 0) 1)))
|
(def! flatten (fn* (l) (let*
|
||||||
'dec (fn* (o) (set-nth! o 0 (- (nth o 0) 1)))
|
(flatten-helper (fn* (l a f) (if (> (count l) 0)
|
||||||
'set (fn* (o n) (set-nth! o 0 n))
|
(f (rest l) (concat a (first l)) f)
|
||||||
'get (fn* (o) (nth o 0))
|
a)))
|
||||||
]))
|
(flatten-helper l [] flatten-helper))))
|
||||||
(def! to_be_saved (with-meta [1] [2]))
|
(add_grammar_rule 'form [ "obj" 'optional_WS 'atom 'optional_WS "{" 'optional_WS 'form 'optional_WS [ 'atom 'optional_WS 'form 'optional_WS ] + "}" ] (fn* (_ _ name _ _ _ constructor _ methods _)
|
||||||
(def! main (fn* () (let* ( a 7
|
(let* (processed_methods (flatten (map (fn* (m) [`'~(nth m 0) (nth m 2)]) methods)))
|
||||||
b [1]
|
`(def! ~name (fn* (& args) (with-meta (apply ~constructor args) [ ,processed_methods ] ))))))
|
||||||
c (with-meta b "yolo") )
|
obj my_constructor {
|
||||||
(do
|
(fn* () [17])
|
||||||
(try*
|
inc (fn* (o) (set-nth! o 0 (+ (nth o 0) 1)))
|
||||||
((fn* () (do
|
dec (fn* (o) (set-nth! o 0 (- (nth o 0) 1)))
|
||||||
(println b)
|
set (fn* (o n) (set-nth! o 0 n))
|
||||||
(set-nth! b 0 2)
|
get (fn* (o) (nth o 0))
|
||||||
(println b)
|
}
|
||||||
(println c)
|
|
||||||
(println (meta c))
|
(println "pre construct")
|
||||||
(println "world")
|
(def! actual_obj (my_constructor))
|
||||||
(println to_be_saved)
|
(println "constructed" actual_obj)
|
||||||
(println (meta to_be_saved))
|
|
||||||
(println "Here in main testing our_obj")
|
(println "here" actual_obj)
|
||||||
(println our_obj)
|
(println "here" (meta actual_obj))
|
||||||
((meta our_obj))
|
|
||||||
(println our_obj)
|
(def! main (fn* () (do
|
||||||
((meta our_obj))
|
|
||||||
(println our_obj)
|
|
||||||
(println "actual_obj" actual_obj)
|
(println "actual_obj" actual_obj)
|
||||||
(method-call actual_obj 'inc)
|
(method-call actual_obj 'inc)
|
||||||
(println "actual_obj" actual_obj)
|
(println "actual_obj" actual_obj)
|
||||||
@@ -54,8 +50,7 @@
|
|||||||
actual_obj.set(1337)
|
actual_obj.set(1337)
|
||||||
(println "actual_obj" actual_obj)
|
(println "actual_obj" actual_obj)
|
||||||
(println "with get " actual_obj.get())
|
(println "with get " actual_obj.get())
|
||||||
a)))
|
0)))
|
||||||
)))))
|
|
||||||
(do
|
(do
|
||||||
(println "interp-main")
|
(println "interp-main")
|
||||||
(main)
|
(main)
|
||||||
|
|||||||
Reference in New Issue
Block a user