Skip to content
This repository has been archived by the owner on Mar 4, 2024. It is now read-only.

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
Thepigcat76 committed Nov 30, 2023
1 parent 9811e27 commit c3842f6
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 20 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ keywords = ["rust", "nexus", "scripting", "language"]
homepage = "https://muffingroup.github.io/MuffinSite/"

[dependencies]
clutils = "0.1.0"
colored = "2.0.4"
maplit = "1.0.2"
20 changes: 8 additions & 12 deletions src/evaluator/enviroment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::object::Object;

type OptionalEnvObj = Option<Box<EnvObj>>;

#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum EnvObj {
OBJ(Obj),
SCOPE(Scope),
Expand All @@ -16,20 +16,20 @@ pub struct Obj {
pub is_const: bool,
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Scope {
objs: HashMap<String, OptionalEnvObj>,
}

impl Scope {
pub fn set(&mut self, name: String, obj: Obj) {
pub fn set(&mut self, name: &String, obj: Obj) {
match self.objs.insert(format!("$var_{}", name), Some(Box::from(EnvObj::OBJ(obj)))) {
Some(_) => (),
None => todo!(),
None => (),
}
}

pub fn get(&mut self, name: String) -> Result<Obj, ()> {
pub fn get(&mut self, name: &String) -> Result<Obj, ()> {
match self.objs.get(&format!("$var_{}", name)) {
Some(obj) => match obj {
Some(val) => match &**val {
Expand All @@ -43,7 +43,7 @@ impl Scope {
}
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Environment {
scope_id: i64,
objs: HashMap<String, EnvObj>,
Expand All @@ -62,7 +62,6 @@ impl Environment {
Some(_) => (),
None => (),
}
println!("{:?}", self.get_global(name))
}

pub fn get_global(&self, name: &String) -> Result<Obj, ()> {
Expand Down Expand Up @@ -95,11 +94,8 @@ impl Environment {
format!("#scope_{}", self.scope_id),
EnvObj::SCOPE(Scope { objs: HashMap::new() }),
) {
Some(obj) => match obj {
EnvObj::OBJ(_) => todo!(),
EnvObj::SCOPE(scope) => scope,
},
None => todo!(),
Some(_) => Scope { objs: HashMap::new() },
None => Scope { objs: HashMap::new() },
}
}
}
59 changes: 55 additions & 4 deletions src/evaluator/evaluator.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::{borrow::BorrowMut, ops::DerefMut};

use crate::{
builtin::builtins::{self, *},
parser::ast::*,
Expand All @@ -9,6 +11,7 @@ use super::{
object::{self, *},
};

#[derive(Debug, Clone)]
pub struct Evaluator {
env: Environment,
/// When scope is None we are in the global scope
Expand Down Expand Up @@ -120,8 +123,27 @@ impl Evaluator {
}

fn eval_identifier(&mut self, ident: &Identifier) -> Object {
match self.cur_scope {
Some(_) => todo!(),
match &mut self.cur_scope {
Some(scope) => match scope.get(&ident.value) {
Ok(obj) => *obj.obj,
Err(_) => match &ident.value {
i if i == &BuiltinType::BOOLEAN.literal() => {
Object::Type(object::Type::BUILTIN(BuiltinType::BOOLEAN))
}
i if i == &BuiltinType::NUMBER.literal() => {
Object::Type(object::Type::BUILTIN(BuiltinType::NUMBER))
}
i if i == &BuiltinType::STRING.literal() => {
Object::Type(object::Type::BUILTIN(BuiltinType::STRING))
}
_ => {
let err = Error::new(format!("Cannot find identifier: {}", ident.value));
println!("scope: {:#?}", self.cur_scope);
throw_error(&err);
Object::Error(err)
}
},
},
None => match self.env.get_global(&ident.value) {
Ok(obj) => *obj.obj,
Err(_) => match &ident.value {
Expand Down Expand Up @@ -562,9 +584,38 @@ impl Evaluator {
builtins::BuiltinFunction::read_input(&func);
Object::BuiltInFunction(func)
}
_ => todo!(),
_ => {
let scope = self.env.create_scope();
self.cur_scope = Some(scope);
let func = match self.env.get_global(&ident.value) {
Ok(obj) => *obj.obj,
Err(_) => todo!(),
};

let func_obj = match func {
Object::Function(func_obj) => func_obj,
_ => todo!(),
};

match &mut self.cur_scope {
Some(scope) => {
for (index, arg) in func_obj.args.iter().enumerate() {
if index < node.args.len() {
self.eval_expression(&Expression::EMPTY);
scope.set(&String::new(), todo!())
// cur_scope.set(&arg.arg.value, Obj { obj: Box::new(cur_arg), is_const: false });
} else {
break;
}
}
}
None => todo!(),
}

todo!()
}
},
_ => todo!("{:?}", node.function),
_ => todo!(),
}
}

Expand Down
8 changes: 4 additions & 4 deletions test/test.nx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var x = 1
foo :: func(x) {
print(x)
}

x = 2

print(x)
foo(0)

0 comments on commit c3842f6

Please sign in to comment.