added function calls, Rust functions, enviroment
This commit is contained in:
parent
867cea5046
commit
58b1b2f94b
5 changed files with 94 additions and 8 deletions
27
src/enviroment.rs
Normal file
27
src/enviroment.rs
Normal file
|
@ -0,0 +1,27 @@
|
|||
use crate::{executor::{MemoryData, MemoryRustFunction}, parser::ASTPart};
|
||||
|
||||
fn to_printed(from: &ASTPart) -> String {
|
||||
match from {
|
||||
ASTPart::String(s) => s.value.clone(),
|
||||
ASTPart::Number(n) => n.value.to_string(),
|
||||
ASTPart::Boolean(b) => b.value.to_string(),
|
||||
ASTPart::Null(_) => String::from("null"),
|
||||
_ => String::new()
|
||||
}
|
||||
}
|
||||
|
||||
fn ugass(args: Vec<ASTPart>) -> ASTPart {
|
||||
let mut out = String::new();
|
||||
for arg in &args {
|
||||
out.push_str(&to_printed(arg));
|
||||
out.push_str(" ");
|
||||
}
|
||||
println!("{}", out);
|
||||
return ASTPart::NOOP;
|
||||
}
|
||||
|
||||
pub fn generate() -> Vec<MemoryData> {
|
||||
let mut out: Vec<MemoryData> = vec![];
|
||||
out.push(MemoryData::RustFunction(MemoryRustFunction { key: String::from("ugass"), func: ugass }));
|
||||
return out;
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
use crate::parser::{ASTPart, AstBool, AstFunction, AstNull, AstNumber, AstString};
|
||||
use crate::{enviroment, parser::{ASTPart, AstBool, AstFunction, AstNull, AstNumber, AstRustFunction, AstString}};
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum State {
|
||||
|
@ -14,7 +14,8 @@ pub enum MemoryData {
|
|||
String(MemoryString),
|
||||
Boolean(MemoryBoolean),
|
||||
Null(MemoryNull),
|
||||
Function(MemoryFunction)
|
||||
Function(MemoryFunction),
|
||||
RustFunction(MemoryRustFunction),
|
||||
}
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct MemoryNumber {
|
||||
|
@ -41,6 +42,11 @@ pub struct MemoryFunction {
|
|||
pub args: Vec<String>,
|
||||
pub body: Vec<ASTPart>
|
||||
}
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct MemoryRustFunction {
|
||||
pub key: String,
|
||||
pub func: fn(Vec<ASTPart>) -> ASTPart
|
||||
}
|
||||
|
||||
pub struct Executor {
|
||||
pub code: Vec<ASTPart>,
|
||||
|
@ -53,10 +59,12 @@ pub struct Executor {
|
|||
|
||||
impl Executor {
|
||||
pub fn new(code: Vec<ASTPart>, max_oppr: usize) -> Executor {
|
||||
let env = enviroment::generate();
|
||||
return Executor {
|
||||
code,
|
||||
pos: 0,
|
||||
memory: vec![
|
||||
env,
|
||||
vec![]
|
||||
],
|
||||
breakpoints: vec![],
|
||||
|
@ -93,6 +101,11 @@ impl Executor {
|
|||
if func.key == key {
|
||||
return ASTPart::Function(AstFunction { args: func.args.clone(), body: func.body.clone(), pos: 0 });
|
||||
}
|
||||
},
|
||||
MemoryData::RustFunction(func) => {
|
||||
if func.key == key {
|
||||
return ASTPart::RustFunction(AstRustFunction { func: func.func, pos: 0 });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -136,6 +149,12 @@ impl Executor {
|
|||
self.memory[current_scope].remove(data);
|
||||
return true;
|
||||
}
|
||||
},
|
||||
MemoryData::RustFunction(func) => {
|
||||
if func.key == key {
|
||||
self.memory[current_scope].remove(data);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -413,8 +432,43 @@ impl Executor {
|
|||
self.memory.pop();
|
||||
}
|
||||
return ASTPart::NOOP;
|
||||
}
|
||||
},
|
||||
ASTPart::Call(call) => {
|
||||
let function = self.process_code(&call.function, op_count);
|
||||
if self.state != State::Running {
|
||||
return ASTPart::NOOP;
|
||||
}
|
||||
match function {
|
||||
ASTPart::Function(func) => {
|
||||
self.memory.push(vec![]);
|
||||
for i in 0..call.args.len() {
|
||||
self.write_memory(func.args[i].clone(), call.args[i].clone(), false);
|
||||
}
|
||||
for opc in &func.body {
|
||||
self.process_code(opc, op_count);
|
||||
if op_count >= &mut self.max_oppr {
|
||||
self.state = State::Error(String::from("Too long without yielding!"));
|
||||
return ASTPart::NOOP;
|
||||
}
|
||||
if self.state != State::Running {
|
||||
return ASTPart::NOOP;
|
||||
}
|
||||
}
|
||||
self.memory.pop();
|
||||
return ASTPart::NOOP;
|
||||
},
|
||||
ASTPart::RustFunction(func) => {
|
||||
let res = (func.func)(call.args.clone());
|
||||
return res;
|
||||
},
|
||||
_ => {
|
||||
self.state = State::Error(format!("Call must have a function, got: {:?} at {}", function, call.pos));
|
||||
return ASTPart::NOOP;
|
||||
}
|
||||
}
|
||||
},
|
||||
_ => {
|
||||
self.state = State::Error(format!("Unknown AST part: {:?}", code));
|
||||
return ASTPart::NOOP;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ use parser::ASTPart;
|
|||
mod lexer;
|
||||
mod parser;
|
||||
mod executor;
|
||||
mod enviroment;
|
||||
|
||||
fn log_ast_part(part: &ASTPart, prefix: String) {
|
||||
match part {
|
||||
|
@ -97,6 +98,7 @@ fn log_ast_part(part: &ASTPart, prefix: String) {
|
|||
}
|
||||
},
|
||||
ASTPart::Continue(cnt) => println!("{}{}: Continue", prefix, cnt.pos),
|
||||
ASTPart::RustFunction(func) => println!("{}{}: Rust Function", prefix, func.pos),
|
||||
ASTPart::NOOP => println!("{}NOOP", prefix)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@ pub enum ASTPart {
|
|||
Break(AstBreak),
|
||||
For(AstFor),
|
||||
Continue(AstContinue),
|
||||
RustFunction(AstRustFunction),
|
||||
NOOP
|
||||
}
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
|
@ -117,6 +118,11 @@ pub struct AstFor {
|
|||
pub struct AstContinue {
|
||||
pub pos: usize
|
||||
}
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct AstRustFunction {
|
||||
pub func: fn(Vec<ASTPart>) -> ASTPart,
|
||||
pub pos: usize
|
||||
}
|
||||
|
||||
fn is_end(input: &Token, end: &Vec<Token>) -> bool {
|
||||
for token in end {
|
||||
|
|
7
test.as
7
test.as
|
@ -1,6 +1,3 @@
|
|||
gethelj a = 1
|
||||
gethelj b = (a + 2) * 3
|
||||
a = 3
|
||||
gethelj d = lőcsve(e,f) {
|
||||
|
||||
ha geny (1 == 1) {
|
||||
ugass(szaft"test"szaft)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue