diff --git a/compiler/src/compile.rs b/compiler/src/compile.rs index 029efda..18c8ba0 100644 --- a/compiler/src/compile.rs +++ b/compiler/src/compile.rs @@ -584,8 +584,6 @@ mod compile_tests { assert_eq!(st.0.len(), 1); } - // this test shows that a literal must be first assigned to a variable before returning it. - // this is a language design decision to ensure that a value has occupied some space in memory before returning it #[test] fn should_compile2() { let lx = Lexer::new(String::from( @@ -822,6 +820,7 @@ mod compile_tests { return downer(n0); }; + @main fn(){ let result @int = downer(3); }@end; diff --git a/compiler/src/compiler_impls.rs b/compiler/src/compiler_impls.rs index 646fbf8..0bd9ab2 100644 --- a/compiler/src/compiler_impls.rs +++ b/compiler/src/compiler_impls.rs @@ -1,5 +1,6 @@ use std::{cell::RefCell, process, rc::Rc}; +use debug_print::debug_println; use either::Either::{self, Left, Right}; use errors::errors::{KarisError, KarisErrorType}; use lexer::tokens::IdentifierKind; @@ -503,6 +504,7 @@ impl Compiler for Node { if let Some(instructions) = left_or_right(rhs, worker.clone(), scope, scope_id) { // update the instructions in the binding table + debug_println!("Assign instructions {:?}", instructions); wrk.add_symbol(binding_key_as_bytes, instructions); None @@ -661,6 +663,8 @@ impl Compiler for Node { let binding_key = random_string_id(); let binding_key_as_bytes = binding_key.as_bytes().to_vec(); + debug_println!("call instructions {:?}", caller_instructions); + let wrk = worker.borrow(); wrk.add_symbol(binding_key_as_bytes.clone(), caller_instructions.clone()); diff --git a/compiler/src/vm_executor.rs b/compiler/src/vm_executor.rs index 26fe3ea..e7db605 100644 --- a/compiler/src/vm_executor.rs +++ b/compiler/src/vm_executor.rs @@ -22,8 +22,6 @@ impl VM { let command: OpCode = command.into(); match command { - OpCode::OpTerminal => Ok(CompileObject::Null), - OpCode::OpAdd | OpCode::OpMinus | OpCode::OpMultiply @@ -116,6 +114,15 @@ impl VM { Ok(binding_value) } + OpCode::OpConstant => { + let op_instruction = instruction.first().unwrap(); + let location = op_instruction.get(5).unwrap(); + let location = *location as usize; + let obj = self.byte_code.constants.get(location).unwrap(); + let obj = obj.clone(); + Ok(obj) + } + OpCode::OpReturn => { let op_instruction = instruction.first().unwrap(); let return_instructions = op_instruction.get(5..op_instruction.len() - 1).unwrap(); @@ -137,15 +144,6 @@ impl VM { self.executor(&binding_instructions, params) } - OpCode::OpConstant => { - let op_instruction = instruction.first().unwrap(); - let location = op_instruction.get(5).unwrap(); - let location = *location as usize; - let obj = self.byte_code.constants.get(location).unwrap(); - let obj = obj.clone(); - Ok(obj) - } - OpCode::OpAddBuiltin => { let op_instruction = instruction.first().unwrap(); @@ -551,7 +549,7 @@ impl VM { let rhs_value = rhs.as_integer().unwrap(); lhs_value < rhs_value } - // conditional AND operation on string return the most significant string length + // conditional OR operation on string return the most significant string length STRING_OBJECT_TYPE => { let lhs_value = lhs.as_string().unwrap(); let rhs_value = rhs.as_string().unwrap(); @@ -738,6 +736,7 @@ impl VM { OpCode::OpBang => panic!(""), OpCode::OpNull + | OpCode::OpTerminal | OpCode::OpMain | OpCode::OpFunctionDef | OpCode::OpGetFunctionParameter diff --git a/console/src/main.rs b/console/src/main.rs index ea9a0cb..9b17dad 100644 --- a/console/src/main.rs +++ b/console/src/main.rs @@ -82,6 +82,7 @@ fn main() -> Result<(), KarisError> { .subcommand( Command::new("script") .about("Executes a Karis script") + .arg(arg!(-p --filepath ).required(false)) .arg_required_else_help(true), ) .subcommand( diff --git a/lexer/src/lexer.rs b/lexer/src/lexer.rs index 7dce1f4..fbd8ae1 100644 --- a/lexer/src/lexer.rs +++ b/lexer/src/lexer.rs @@ -781,7 +781,7 @@ fn is_alphanumeric_only(i: &str) -> bool { } // checks if the identifier is composed of Integers only -// these Integers will be parsed correctly when performing arthemetic operations +// these Integers will be parsed correctly when performing operations fn is_integers_only(i: &str) -> bool { for x in i.as_bytes() { if !is_digit(*x) { diff --git a/parser/src/registry_led.rs b/parser/src/registry_led.rs index 7613d94..68707a7 100644 --- a/parser/src/registry_led.rs +++ b/parser/src/registry_led.rs @@ -229,7 +229,7 @@ impl TokenRegistry { /// Evaluates the RHS of an expression /// Checks if the RHS is /// - a literal => string, bool, int, array - /// - arthemetic expression + /// - expression /// - function definition expression /// - function call expression /// @@ -321,7 +321,7 @@ impl TokenRegistry { } } - // here, the RHS is a fully-qualified expression. That is either a function declaration, arthemetic expression or function call + // here, the RHS is a fully-qualified expression. That is either a function declaration, expression or function call // we therefore parse the expression then return it as the RHS of `=` let res = Parser::expression(0x00, token_index + 0x01, bucket.clone()); if res.is_err() { diff --git a/parser/src/registry_nud.rs b/parser/src/registry_nud.rs index f4e16a1..882d67b 100644 --- a/parser/src/registry_nud.rs +++ b/parser/src/registry_nud.rs @@ -424,7 +424,7 @@ impl TokenRegistry { let res = Parser::expression(0, index + 0x01, bucket.clone())?; // we re-organize the object. We want the UNION kind to be the top-level object, not the root of the - // enclosed arthemetic expression. + // enclosed expression. let object = reorganize_parenthesis_object(res.0.clone()); Ok((object, res.1)) diff --git a/testdata/00/test01.kr b/testdata/00/test01.kr index 50a4969..bb99ea7 100644 --- a/testdata/00/test01.kr +++ b/testdata/00/test01.kr @@ -2,6 +2,10 @@ let add @int = fn(x @int, y @int){ return x + y; }; -let x @int = 5; -let y @int = 7; -let sum @int = x + y; +@main fn(){ + let x @int = 5; + let y @int = 7; + + let sum @int = add(x,y); + print(sum); +}@end; diff --git a/testdata/00/test03.kr b/testdata/00/test03.kr index d032480..0a5bfaf 100644 --- a/testdata/00/test03.kr +++ b/testdata/00/test03.kr @@ -1,2 +1,7 @@ -let num1 @int = 1; -let num2 @int = 2; \ No newline at end of file +fun main() { + if 7 % 2 == 0 { + println("7 is even") + } else { + println("7 is odd") + } +}