Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions compiler/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -822,6 +820,7 @@ mod compile_tests {
return downer(n0);
};


@main fn(){
let result @int = downer(3);
}@end;
Expand Down
4 changes: 4 additions & 0 deletions compiler/src/compiler_impls.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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());

Expand Down
23 changes: 11 additions & 12 deletions compiler/src/vm_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ impl VM {
let command: OpCode = command.into();

match command {
OpCode::OpTerminal => Ok(CompileObject::Null),

OpCode::OpAdd
| OpCode::OpMinus
| OpCode::OpMultiply
Expand Down Expand Up @@ -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();
Expand All @@ -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();

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -738,6 +736,7 @@ impl VM {
OpCode::OpBang => panic!(""),

OpCode::OpNull
| OpCode::OpTerminal
| OpCode::OpMain
| OpCode::OpFunctionDef
| OpCode::OpGetFunctionParameter
Expand Down
1 change: 1 addition & 0 deletions console/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ fn main() -> Result<(), KarisError> {
.subcommand(
Command::new("script")
.about("Executes a Karis script")
.arg(arg!(-p --filepath <PATH>).required(false))
.arg_required_else_help(true),
)
.subcommand(
Expand Down
2 changes: 1 addition & 1 deletion lexer/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions parser/src/registry_led.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion parser/src/registry_nud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
10 changes: 7 additions & 3 deletions testdata/00/test01.kr
Original file line number Diff line number Diff line change
Expand Up @@ -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;
9 changes: 7 additions & 2 deletions testdata/00/test03.kr
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
let num1 @int = 1;
let num2 @int = 2;
fun main() {
if 7 % 2 == 0 {
println("7 is even")
} else {
println("7 is odd")
}
}
Loading