diff options
| -rw-r--r-- | Cargo.lock | 231 | ||||
| -rw-r--r-- | Cargo.toml | 2 | ||||
| -rw-r--r-- | samples/sample1.code | 11 | ||||
| -rw-r--r-- | samples/sample2.code | 1 | ||||
| -rw-r--r-- | src/main.rs | 84 |
5 files changed, 77 insertions, 252 deletions
@@ -3,42 +3,6 @@ version = 3 [[package]] -name = "addr2line" -version = "0.17.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b9ecd88a8c8378ca913a680cd98f0f13ac67383d35993f86c90a70e3f137816b" -dependencies = [ - "gimli", -] - -[[package]] -name = "adler" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" - -[[package]] -name = "autocfg" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" - -[[package]] -name = "backtrace" -version = "0.3.63" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "321629d8ba6513061f26707241fa9bc89524ff1cd7a915a97ef0c62c666ce1b6" -dependencies = [ - "addr2line", - "cc", - "cfg-if", - "libc", - "miniz_oxide", - "object", - "rustc-demangle", -] - -[[package]] name = "block-buffer" version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -72,18 +36,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" [[package]] -name = "cc" -version = "1.0.72" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22a9137b95ea06864e018375b72adfb7db6e6f68cfc8df5a04d00288050485ee" - -[[package]] -name = "cfg-if" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" - -[[package]] name = "digest" version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -93,50 +45,12 @@ dependencies = [ ] [[package]] -name = "either" -version = "1.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" - -[[package]] -name = "failure" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d32e9bd16cc02eae7db7ef620b392808b89f6a5e16bb3497d159c6b92a0f4f86" -dependencies = [ - "backtrace", - "failure_derive", -] - -[[package]] -name = "failure_derive" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa4da3c766cd7a0db8242e326e9e4e081edd567072893ed320008189715366a4" -dependencies = [ - "proc-macro2 1.0.32", - "quote 1.0.10", - "syn 1.0.81", - "synstructure", -] - -[[package]] name = "fake-simd" version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" [[package]] -name = "from-pest" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aba9389cedcba1fb3a2aa2ed00f584f2606bce8e0106614a17327a24513bc60f" -dependencies = [ - "pest", - "void", -] - -[[package]] name = "generic-array" version = "0.12.4" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -146,58 +60,12 @@ dependencies = [ ] [[package]] -name = "gimli" -version = "0.26.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78cc372d058dcf6d5ecd98510e7fbc9e5aec4d21de70f65fea8fecebcd881bd4" - -[[package]] -name = "itertools" -version = "0.7.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d47946d458e94a1b7bcabbf6521ea7c037062c81f534615abcad76e84d4970d" -dependencies = [ - "either", -] - -[[package]] -name = "libc" -version = "0.2.107" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fbe5e23404da5b4f555ef85ebed98fb4083e55a00c317800bc2a50ede9f3d219" - -[[package]] name = "maplit" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3e2e65a1a2e43cfcb47a895c4c8b10d1f4a61097f9f254f183aee60cad9c651d" [[package]] -name = "memchr" -version = "2.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" - -[[package]] -name = "miniz_oxide" -version = "0.4.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a92518e98c078586bc6c934028adcca4c92a53d6a958196de835170a01d84e4b" -dependencies = [ - "adler", - "autocfg", -] - -[[package]] -name = "object" -version = "0.27.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "67ac1d3f9a1d3616fd9a60c8d74296f22406a238b6a72f5cc1e6f314df4ffbf9" -dependencies = [ - "memchr", -] - -[[package]] name = "opaque-debug" version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -213,19 +81,6 @@ dependencies = [ ] [[package]] -name = "pest-ast" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fbf404899169771dd6a32c84248b83cd67a26cc7cc957aac87661490e1227e4" -dependencies = [ - "itertools", - "proc-macro2 0.4.30", - "quote 0.6.13", - "single", - "syn 0.15.44", -] - -[[package]] name = "pest_derive" version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -243,9 +98,9 @@ checksum = "99b8db626e31e5b81787b9783425769681b347011cc59471e33ea46d2ea0cf55" dependencies = [ "pest", "pest_meta", - "proc-macro2 1.0.32", - "quote 1.0.10", - "syn 1.0.81", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -261,57 +116,31 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "0.4.30" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759" -dependencies = [ - "unicode-xid 0.1.0", -] - -[[package]] -name = "proc-macro2" version = "1.0.32" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ba508cc11742c0dc5c1659771673afbab7a0efab23aa17e854cbab0837ed0b43" dependencies = [ - "unicode-xid 0.2.2", + "unicode-xid", ] [[package]] name = "prog-lang-dev1" version = "0.1.0" dependencies = [ - "from-pest", "pest", - "pest-ast", "pest_derive", ] [[package]] name = "quote" -version = "0.6.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ce23b6b870e8f94f81fb0a363d65d86675884b34a09043c81e5562f11c1f8e1" -dependencies = [ - "proc-macro2 0.4.30", -] - -[[package]] -name = "quote" version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "38bc8cc6a5f2e3655e0899c1b848643b2562f853f114bfec7be120678e3ace05" dependencies = [ - "proc-macro2 1.0.32", + "proc-macro2", ] [[package]] -name = "rustc-demangle" -version = "0.1.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ef03e0a2b150c7a90d01faf6254c9c48a41e95fb2a8c2ac1c6f0d2b9aefc342" - -[[package]] name = "sha-1" version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -324,46 +153,14 @@ dependencies = [ ] [[package]] -name = "single" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bd5add732a1ab689845591a1b50339cf5310b563e08dc5813c65991f30369ea2" -dependencies = [ - "failure", -] - -[[package]] -name = "syn" -version = "0.15.44" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ca4b3b69a77cbe1ffc9e198781b7acb0c7365a883670e8f1c1bc66fba79a5c5" -dependencies = [ - "proc-macro2 0.4.30", - "quote 0.6.13", - "unicode-xid 0.1.0", -] - -[[package]] name = "syn" version = "1.0.81" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f2afee18b8beb5a596ecb4a2dce128c719b4ba399d34126b9e4396e3f9860966" dependencies = [ - "proc-macro2 1.0.32", - "quote 1.0.10", - "unicode-xid 0.2.2", -] - -[[package]] -name = "synstructure" -version = "0.12.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f36bdaa60a83aca3921b5259d5400cbf5e90fc51931376a9bd4a0eb79aa7210f" -dependencies = [ - "proc-macro2 1.0.32", - "quote 1.0.10", - "syn 1.0.81", - "unicode-xid 0.2.2", + "proc-macro2", + "quote", + "unicode-xid", ] [[package]] @@ -380,18 +177,6 @@ checksum = "56dee185309b50d1f11bfedef0fe6d036842e3fb77413abef29f8f8d1c5d4c1c" [[package]] name = "unicode-xid" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" - -[[package]] -name = "unicode-xid" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" - -[[package]] -name = "void" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" @@ -8,5 +8,3 @@ edition = "2021" [dependencies] pest = "2.0" pest_derive = "2.0" -pest-ast = "0.3" -from-pest = "0.3.1" diff --git a/samples/sample1.code b/samples/sample1.code index b941226..650b0ea 100644 --- a/samples/sample1.code +++ b/samples/sample1.code @@ -1,10 +1 @@ -foo = "foo"; -number-of-the-beast = 666; -cool-number = number-of-the-beast; # value lookup -num = int.zero; # builtin value -int.double = int.multiply 2; -list.multiply-all = reduce int.multiply 1; - -ifelse (string.eq? foo "bar") - (int.double 1) # if foo = "bar", calc 1 * 2 - number-of-the-beast # else, number of the beast +(id id) 1 diff --git a/samples/sample2.code b/samples/sample2.code new file mode 100644 index 0000000..eedd45d --- /dev/null +++ b/samples/sample2.code @@ -0,0 +1 @@ +lempi-numero = int.zero; diff --git a/src/main.rs b/src/main.rs index f8e18cb..72b60e2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -127,53 +127,100 @@ mod ast { mod runtime { use super::ast; use std::collections::HashMap; + use std::rc::Rc; + use std::sync::atomic::{AtomicUsize, Ordering}; #[derive(Debug, Clone)] pub enum Value { Integer(i64), String(String), + Var(usize), + Function(usize, Rc<Value>), } + static VAR_ID_INC: AtomicUsize = AtomicUsize::new(0); + fn try_evaluate_builtin(symbol: &ast::Symbol) -> Option<Value> { match symbol.as_str() { "int.zero" => Some(Value::Integer(0)), + "id" => { + let v = VAR_ID_INC.load(Ordering::Relaxed); + VAR_ID_INC.store(v + 1, Ordering::Relaxed); + Some(Value::Function(v, Rc::new(Value::Var(v)))) + } _ => None, } } - fn evaluate_expr(symbol_table: &HashMap<String, Value>, expression: &ast::Expression) -> Value { + fn try_apply_function(func_rc: Rc<Value>, arg: Rc<Value>, bound_v: Option<usize>) -> Rc<Value> { + let func = &*func_rc; + match func { + Value::Function(func_v, body_rc) => { + let v1 = bound_v.unwrap_or(*func_v); + let body = &**body_rc; + match body { + Value::Var(v2) => { + if v1 == *v2 { + arg + } else { + (*body_rc).clone() + } + } + Value::Function(_, _) => try_apply_function((*body_rc).clone(), arg, Some(v1)), + _ => (*body_rc).clone(), + } + } + _ => func_rc, + } + } + + fn evaluate_expr( + symbol_table: &HashMap<String, Rc<Value>>, + expression: &ast::Expression, + ) -> Rc<Value> { // println!("[runtime] evaluating expression"); let head = &expression[0]; let tail = &expression[1..]; let arity = tail.len(); match head { ast::ExpressionInner::IntegerLiteral(value) => match arity { - 0 => Value::Integer(*value), + 0 => Rc::new(Value::Integer(*value)), _ => panic!("[runtime] cannot apply integer: {}", value), }, ast::ExpressionInner::StringLiteral(value) => match arity { - 0 => Value::String(value.clone()), + 0 => Rc::new(Value::String(value.clone())), _ => panic!("[runtime] cannot apply string: {}", value), }, - ast::ExpressionInner::Symbol(value) => { - let builtin_value = try_evaluate_builtin(value); - if builtin_value.is_some() { - return builtin_value.unwrap(); - } + ast::ExpressionInner::Symbol(value) => match arity { + 0 => { + let builtin_value = try_evaluate_builtin(value); + if builtin_value.is_some() { + return Rc::new(builtin_value.unwrap()); + } - let table_lookup_value = symbol_table.get(value); - if table_lookup_value.is_some() { - return table_lookup_value.unwrap().clone(); - } + let table_lookup_value = symbol_table.get(value); + if table_lookup_value.is_some() { + let lookup_rc = table_lookup_value.unwrap(); + return lookup_rc.clone(); + } - Value::String(String::from("dummy value")) - } + Rc::new(Value::String(String::from("dummy value"))) + } + 1 => { + // let arg = &tail[0]; + // let applied = try_apply_function(value, arg, None); + panic!("should apply function here!") + } + _ => unreachable!( + "[runtime] expression arity >= 2! there must be an error in AST generation" + ), + }, ast::ExpressionInner::Expression(expression) => evaluate_expr(symbol_table, expression), } } pub fn evaluate(program: &ast::Program) { - let mut symbol_table: HashMap<String, Value> = HashMap::new(); + let mut symbol_table: HashMap<String, Rc<Value>> = HashMap::new(); for statement in program { match statement { @@ -200,9 +247,12 @@ mod runtime { fn main() { use ast::Program; use pest::Parser; + use std::env; use std::fs; - let unparsed_file = fs::read_to_string("samples/sample1.code").expect("cannot read file"); + let script_path = env::args().nth(1).expect("no script file specified"); + + let unparsed_file = fs::read_to_string(script_path).expect("cannot read file"); let parse_tree_result = parser::Parser::parse(parser::Rule::program, &unparsed_file); if parse_tree_result.is_err() { @@ -212,7 +262,7 @@ fn main() { let mut parse_tree = parse_tree_result.unwrap(); - // println!("parse tree = {:#?}", parse_tree); + println!("parse tree = {:#?}", parse_tree); let syntax_tree: Program = ast::from_parse_tree(&mut parse_tree); println!("syntax tree = {:#?}", syntax_tree); |
