186 lines
No EOL
5.9 KiB
Rust
186 lines
No EOL
5.9 KiB
Rust
use crate::lexer::{Token, TokenType};
|
|
|
|
#[derive(Debug)]
|
|
pub enum ASTPart {
|
|
String(AstString),
|
|
Number(AstNumber),
|
|
Assigment(AstAssigment),
|
|
Operation(AstOperation),
|
|
VarRead(AstVarRead),
|
|
Call(AstCall),
|
|
NOOP
|
|
}
|
|
#[derive(Debug)]
|
|
pub struct AstString {
|
|
pub value: String,
|
|
pub pos: usize
|
|
}
|
|
#[derive(Debug)]
|
|
pub struct AstNumber {
|
|
pub value: i64,
|
|
pub pos: usize
|
|
}
|
|
#[derive(Debug)]
|
|
pub struct AstAssigment {
|
|
pub variable: String,
|
|
pub value: Box<ASTPart>,
|
|
pub pos: usize
|
|
}
|
|
#[derive(Debug)]
|
|
pub struct AstOperation {
|
|
pub operator: String,
|
|
pub left: Box<ASTPart>,
|
|
pub right: Box<ASTPart>,
|
|
pub pos: usize
|
|
}
|
|
#[derive(Debug)]
|
|
pub struct AstVarRead {
|
|
pub variable: String,
|
|
pub pos: usize
|
|
}
|
|
#[derive(Debug)]
|
|
pub struct AstCall {
|
|
pub function: Box<ASTPart>,
|
|
pub args: Vec<ASTPart>,
|
|
pub pos: usize
|
|
}
|
|
|
|
fn is_end(input: &Token, end: &Vec<Token>) -> bool {
|
|
for token in end {
|
|
if input.typ == token.typ && (token.value == "" || input.value == token.value) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
fn read_call(variable: ASTPart, pos: &mut usize, input: &Vec<Token>) -> ASTPart {
|
|
let mut args: Vec<ASTPart> = vec![];
|
|
*pos += 1;
|
|
let start_pos = *pos;
|
|
while pos < &mut input.len() {
|
|
let token = &input[*pos];
|
|
if token.typ == TokenType::SEPARATOR && token.value == "," {
|
|
*pos += 1;
|
|
continue;
|
|
}
|
|
if token.typ == TokenType::SEPARATOR && token.value == String::from(")") {
|
|
*pos += 1;
|
|
break;
|
|
}
|
|
let ends: Vec<Token> = vec![
|
|
Token { typ: TokenType::SEPARATOR, value: String::from(","), pos: 0 },
|
|
Token { typ: TokenType::SEPARATOR, value: String::from(")"), pos: 0 }
|
|
];
|
|
let arg = read_exp(pos, input, &ends, &ends);
|
|
args.push(arg);
|
|
}
|
|
return ASTPart::Call(AstCall { function: Box::new(variable), args: args, pos: start_pos });
|
|
}
|
|
|
|
fn shunt(input: Vec<ASTPart>) -> ASTPart {
|
|
for part in input {
|
|
|
|
}
|
|
return ASTPart::NOOP;
|
|
}
|
|
fn read_exp(pos: &mut usize, input: &Vec<Token>, ends: &Vec<Token>, parse_ends: &Vec<Token>) -> ASTPart {
|
|
let mut expressions: Vec<ASTPart> = vec![];
|
|
while pos < &mut input.len() {
|
|
let token = &input[*pos];
|
|
let mut next_token = &Token {
|
|
typ: TokenType::OPEND,
|
|
value: String::from("END"),
|
|
pos: 0
|
|
};
|
|
if *pos+1 < input.len() {
|
|
next_token = &input[*pos+1]
|
|
}
|
|
if is_end(token, &parse_ends) {
|
|
break;
|
|
}
|
|
*pos += 1;
|
|
if is_end(token, ends) {
|
|
break;
|
|
}
|
|
if token.typ == TokenType::STRING {
|
|
expressions.push(ASTPart::String(AstString { value: token.value.clone(), pos: token.pos }));
|
|
} else if token.typ == TokenType::NUMBER {
|
|
expressions.push(ASTPart::Number(AstNumber { value: token.value.parse().unwrap(), pos: token.pos }));
|
|
} else if token.typ == TokenType::IDENTIFIER {
|
|
if next_token.typ == TokenType::SEPARATOR && next_token.value == "(" {
|
|
let var = ASTPart::VarRead(AstVarRead { variable: token.value.clone(), pos: token.pos });
|
|
expressions.push(read_call(var, pos, input));
|
|
} else {
|
|
expressions.push(ASTPart::VarRead(AstVarRead { variable: token.value.clone(), pos: token.pos }));
|
|
}
|
|
} else if token.typ == TokenType::OPERATOR {
|
|
expressions.push(ASTPart::Operation(AstOperation { operator: token.value.clone(), left: Box::new(ASTPart::NOOP), right: Box::new(ASTPart::NOOP), pos: token.pos }));
|
|
} else {
|
|
panic!("Unexpected {:?}({}) at {}", token.typ, token.value, token.pos);
|
|
}
|
|
}
|
|
println!("{:?}", expressions);
|
|
let shunted = shunt(expressions);
|
|
return ASTPart::NOOP;
|
|
}
|
|
|
|
fn next_operation(pos: &mut usize, input: &Vec<Token>, op_ends: &Vec<Token>, parse_ends: &Vec<Token>) -> ASTPart {
|
|
let token = &input[*pos];
|
|
if is_end(token, &parse_ends) {
|
|
return ASTPart::NOOP;
|
|
}
|
|
*pos += 1;
|
|
if is_end(token, &op_ends) {
|
|
return ASTPart::NOOP;
|
|
}
|
|
if token.typ == TokenType::KEYWORD {
|
|
if token.value == "gethelj" {
|
|
let variable = &input[*pos];
|
|
*pos += 1;
|
|
if variable.typ != TokenType::IDENTIFIER {
|
|
panic!("Unexpected {:?} at {}", variable.typ, variable.pos)
|
|
}
|
|
let eq = &input[*pos];
|
|
if eq.typ == TokenType::SEPARATOR && eq.value == "=" {
|
|
*pos += 1;
|
|
}
|
|
let value = read_exp(pos, input, op_ends, parse_ends);
|
|
return ASTPart::Assigment(AstAssigment { variable: variable.value.clone(), value: Box::new(value), pos: token.pos });
|
|
} else {
|
|
panic!("Unexpected {:?}({}) at {}", token.typ, token.value, token.pos);
|
|
}
|
|
} else {
|
|
panic!("Unexpected {:?}({}) at {}", token.typ, token.value, token.pos);
|
|
}
|
|
}
|
|
|
|
fn parse_internal(input: Vec<Token>, op_ends: Vec<Token>, parse_ends: Vec<Token>) -> Vec<ASTPart> {
|
|
let mut out: Vec<ASTPart> = vec![];
|
|
let mut pos = 0;
|
|
while pos < input.len() {
|
|
let op = next_operation(&mut pos, &input, &op_ends, &parse_ends);
|
|
match op {
|
|
ASTPart::NOOP => {},
|
|
_ => {
|
|
out.push(op);
|
|
}
|
|
}
|
|
if is_end(&input[pos], &parse_ends) {
|
|
break;
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
pub fn parse(input: Vec<Token>) -> Vec<ASTPart> {
|
|
let op_ends: Vec<Token> = vec![
|
|
Token { typ: TokenType::OPEND, value: String::from("\n"), pos: 0 },
|
|
Token { typ: TokenType::OPEND, value: String::from(";"), pos: 0 },
|
|
Token { typ: TokenType::OPEND, value: String::from("EOF"), pos: 0 }
|
|
];
|
|
let parse_ends: Vec<Token> = vec![
|
|
Token { typ: TokenType::OPEND, value: String::from("EOF"), pos: 0 }
|
|
];
|
|
let out = parse_internal(input, op_ends, parse_ends);
|
|
return out;
|
|
} |