2023-09-16 22:21:53 +02:00
|
|
|
use std::collections::{HashMap, HashSet};
|
|
|
|
|
|
|
|
use crate::ast::*;
|
|
|
|
use crate::tokeniser::Keyword;
|
|
|
|
use crate::tokeniser::Token;
|
|
|
|
|
|
|
|
fn parse_math(tokens: &Vec<Token>) -> ValueNode {
|
|
|
|
let mut prioritizing_order: HashMap<i64, HashSet<Token>> = HashMap::new();
|
|
|
|
prioritizing_order.insert(0, HashSet::from([Token::Plus, Token::Minus]));
|
|
|
|
|
|
|
|
let mut ordered_binops: Vec<Token> = Vec::new();
|
|
|
|
let mut ordered_binops_indexes: Vec<usize> = Vec::new();
|
|
|
|
|
|
|
|
if tokens.len() == 1 {
|
|
|
|
return match &tokens[0] {
|
|
|
|
Token::Number(n) => ValueNode::Literal(Literal::Int(n.parse::<i64>().unwrap())),
|
2023-09-17 16:27:06 +02:00
|
|
|
Token::Identifier(identifier) => ValueNode::Variable(identifier.to_owned()),
|
2023-09-16 22:21:53 +02:00
|
|
|
_ => todo!(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
for j in prioritizing_order.keys() {
|
|
|
|
let mut i = 0;
|
|
|
|
let current_order_binops = prioritizing_order.get(j).unwrap();
|
|
|
|
|
|
|
|
while i < tokens.len() {
|
|
|
|
for binop in current_order_binops {
|
|
|
|
if &tokens[i] == binop {
|
|
|
|
ordered_binops.push(binop.to_owned());
|
|
|
|
ordered_binops_indexes.push(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let binop = ordered_binops.pop().unwrap();
|
|
|
|
let binop_index = ordered_binops_indexes.pop().unwrap();
|
|
|
|
|
|
|
|
let mut a: Vec<Token> = Vec::new();
|
|
|
|
let mut b: Vec<Token> = Vec::new();
|
|
|
|
|
|
|
|
let mut i = 0;
|
|
|
|
while i < tokens.len() {
|
|
|
|
if i < binop_index {
|
|
|
|
a.push(tokens[i].clone());
|
|
|
|
}
|
|
|
|
if i > binop_index {
|
|
|
|
b.push(tokens[i].clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return match binop {
|
|
|
|
Token::Plus => ValueNode::Operator(Operator::Add(
|
|
|
|
Box::new(parse_math(&a)),
|
|
|
|
Box::new(parse_math(&b)),
|
|
|
|
)),
|
|
|
|
Token::Minus => ValueNode::Operator(Operator::Sub(
|
|
|
|
Box::new(parse_math(&a)),
|
|
|
|
Box::new(parse_math(&b)),
|
|
|
|
)),
|
|
|
|
_ => todo!(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-09-22 17:06:37 +02:00
|
|
|
fn parse_basic(tokens: &Vec<Token>, i: &mut usize) -> Expression {
|
|
|
|
let literal = match &tokens[*i] {
|
|
|
|
Token::Identifier(identifier) => Expression::Variable(identifier.to_owned()),
|
|
|
|
Token::Number(number) => Expression::Literal(Literal::Int(number.parse::<i64>().unwrap())),
|
|
|
|
_ => todo!(),
|
|
|
|
};
|
|
|
|
*i += 1;
|
|
|
|
literal
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_grouping(tokens: &Vec<Token>, i: &mut usize) -> Expression {
|
|
|
|
match &tokens[*i] {
|
|
|
|
Token::OpenParanthesis => {
|
|
|
|
*i += 1;
|
|
|
|
}
|
|
|
|
_ => todo!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let inner_expression = parse_expression(tokens, i);
|
|
|
|
|
|
|
|
match &tokens[*i] {
|
|
|
|
Token::CloseParanthesis => {}
|
|
|
|
_ => todo!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
inner_expression
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: replace parse_math with this recursive function
|
|
|
|
pub fn parse_expression(tokens: &Vec<Token>, i: &mut usize) -> Expression {
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum OpExpr {
|
|
|
|
Expression(Expression),
|
|
|
|
BinOp(BinOp),
|
|
|
|
Unary(Unary),
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut in_expressions_and_operators: Vec<OpExpr> = Vec::new();
|
|
|
|
|
|
|
|
loop {
|
|
|
|
in_expressions_and_operators.push(match &tokens[*i] {
|
|
|
|
Token::OpenParanthesis => OpExpr::Expression(parse_grouping(tokens, i)),
|
|
|
|
Token::Plus => {
|
|
|
|
*i += 1;
|
|
|
|
OpExpr::BinOp(BinOp::Plus)
|
|
|
|
}
|
|
|
|
Token::Minus => {
|
|
|
|
*i += 1;
|
|
|
|
OpExpr::BinOp(BinOp::Minus)
|
|
|
|
}
|
|
|
|
Token::Ampersand => {
|
|
|
|
*i += 1;
|
|
|
|
OpExpr::Unary(Unary::ValueFrom)
|
|
|
|
}
|
|
|
|
Token::Star => {
|
|
|
|
*i += 1;
|
|
|
|
OpExpr::Unary(Unary::PointerTo)
|
|
|
|
}
|
|
|
|
Token::Number(_) => OpExpr::Expression(parse_basic(tokens, i)),
|
|
|
|
_other => break,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
dbg!(in_expressions_and_operators);
|
|
|
|
Expression::Variable("a".to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_statements(tokens: &Vec<Token>, i: &mut usize) -> Vec<Statement> {
|
|
|
|
dbg!(&i);
|
2023-09-17 21:52:14 +02:00
|
|
|
let mut statements = Vec::new();
|
2023-09-22 17:06:37 +02:00
|
|
|
while *i < tokens.len() {
|
|
|
|
statements.push(match &tokens[*i] {
|
2023-09-17 21:52:14 +02:00
|
|
|
Token::OpenSquiglyBracket => {
|
2023-09-22 17:06:37 +02:00
|
|
|
*i += 1;
|
|
|
|
let statements = parse_statements(tokens, i);
|
2023-09-17 21:52:14 +02:00
|
|
|
Statement::Scope(statements)
|
2023-09-17 16:27:06 +02:00
|
|
|
}
|
2023-09-17 21:52:14 +02:00
|
|
|
Token::Keyword(Keyword::Let) => {
|
2023-09-22 17:06:37 +02:00
|
|
|
*i += 1;
|
|
|
|
let name = match tokens[*i].clone() {
|
2023-09-17 21:52:14 +02:00
|
|
|
Token::Identifier(identifier) => identifier,
|
|
|
|
_ => todo!(),
|
|
|
|
};
|
|
|
|
|
2023-09-22 17:06:37 +02:00
|
|
|
*i += 1;
|
|
|
|
match tokens[*i].clone() {
|
2023-09-17 21:52:14 +02:00
|
|
|
Token::Equals => {}
|
|
|
|
_ => todo!(),
|
|
|
|
};
|
2023-09-16 22:21:53 +02:00
|
|
|
|
2023-09-22 17:06:37 +02:00
|
|
|
*i += 1;
|
2023-09-16 22:21:53 +02:00
|
|
|
|
2023-09-17 21:52:14 +02:00
|
|
|
let mut declaration_tokens: Vec<Token> = Vec::new();
|
2023-09-22 17:06:37 +02:00
|
|
|
while tokens[*i] != Token::Semi {
|
|
|
|
declaration_tokens.push(tokens[*i].clone());
|
|
|
|
*i += 1;
|
2023-09-16 22:21:53 +02:00
|
|
|
}
|
2023-09-17 21:52:14 +02:00
|
|
|
Statement::VariableDeclaration(name, parse_math(&declaration_tokens))
|
2023-09-16 22:21:53 +02:00
|
|
|
}
|
2023-09-17 21:52:14 +02:00
|
|
|
Token::Keyword(Keyword::Assign) => {
|
2023-09-22 17:06:37 +02:00
|
|
|
*i += 1;
|
|
|
|
let name = match tokens[*i].clone() {
|
2023-09-17 21:52:14 +02:00
|
|
|
Token::Identifier(identifier) => identifier,
|
|
|
|
_ => todo!(),
|
|
|
|
};
|
2023-09-16 22:21:53 +02:00
|
|
|
|
2023-09-22 17:06:37 +02:00
|
|
|
*i += 1;
|
|
|
|
match tokens[*i].clone() {
|
2023-09-17 21:52:14 +02:00
|
|
|
Token::Equals => {}
|
|
|
|
_ => todo!(),
|
|
|
|
};
|
2023-09-16 22:21:53 +02:00
|
|
|
|
2023-09-22 17:06:37 +02:00
|
|
|
*i += 1;
|
2023-09-16 22:21:53 +02:00
|
|
|
|
2023-09-17 21:52:14 +02:00
|
|
|
let mut declaration_tokens: Vec<Token> = Vec::new();
|
2023-09-22 17:06:37 +02:00
|
|
|
while tokens[*i] != Token::Semi {
|
|
|
|
declaration_tokens.push(tokens[*i].clone());
|
|
|
|
*i += 1;
|
2023-09-16 22:21:53 +02:00
|
|
|
}
|
2023-09-17 21:52:14 +02:00
|
|
|
Statement::VariableAssignment(name, parse_math(&declaration_tokens))
|
2023-09-16 22:21:53 +02:00
|
|
|
}
|
2023-09-17 21:52:14 +02:00
|
|
|
Token::Identifier(identifier) => {
|
2023-09-22 17:06:37 +02:00
|
|
|
*i += 1;
|
2023-09-17 21:52:14 +02:00
|
|
|
|
|
|
|
let mut declaration_tokens: Vec<Vec<Token>> = Vec::new();
|
|
|
|
|
2023-09-22 17:06:37 +02:00
|
|
|
*i += 1;
|
|
|
|
while tokens[*i] != Token::CloseParanthesis {
|
2023-09-17 21:52:14 +02:00
|
|
|
let mut argument_tokens: Vec<Token> = Vec::new();
|
2023-09-22 17:06:37 +02:00
|
|
|
while tokens[*i] != Token::Comma {
|
|
|
|
argument_tokens.push(tokens[*i].clone());
|
|
|
|
*i += 1;
|
2023-09-17 21:52:14 +02:00
|
|
|
}
|
|
|
|
declaration_tokens.push(argument_tokens);
|
2023-09-22 17:06:37 +02:00
|
|
|
*i += 1;
|
2023-09-17 21:52:14 +02:00
|
|
|
}
|
2023-09-16 22:21:53 +02:00
|
|
|
|
2023-09-22 17:06:37 +02:00
|
|
|
*i += 1;
|
2023-09-17 21:52:14 +02:00
|
|
|
|
|
|
|
let mut arguments: Vec<ValueNode> = Vec::new();
|
2023-09-16 22:21:53 +02:00
|
|
|
|
2023-09-17 21:52:14 +02:00
|
|
|
for token_vec in &declaration_tokens {
|
|
|
|
if token_vec == &Vec::new() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
arguments.push(parse_math(token_vec));
|
|
|
|
}
|
2023-09-16 22:21:53 +02:00
|
|
|
|
2023-09-17 21:52:14 +02:00
|
|
|
Statement::FunctionCall(identifier.to_owned(), arguments)
|
|
|
|
}
|
|
|
|
Token::CloseSquiglyBracket => {
|
2023-09-22 17:06:37 +02:00
|
|
|
*i += 1;
|
|
|
|
return statements;
|
2023-09-17 21:52:14 +02:00
|
|
|
}
|
|
|
|
Token::Semi => {
|
2023-09-22 17:06:37 +02:00
|
|
|
*i += 1;
|
2023-09-17 21:52:14 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
_other => {
|
|
|
|
dbg!(_other);
|
2023-09-22 17:06:37 +02:00
|
|
|
dbg!(i);
|
2023-09-17 21:52:14 +02:00
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2023-09-22 17:06:37 +02:00
|
|
|
*i += 1;
|
|
|
|
statements
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parse_arguments(tokens: &Vec<Token>, i: &mut usize) -> Vec<String> {
|
|
|
|
let mut argument_variables = Vec::new();
|
|
|
|
while tokens[*i] != Token::OpenSquiglyBracket {
|
|
|
|
match &tokens[*i] {
|
|
|
|
Token::Identifier(identifier) => argument_variables.push(identifier.to_owned()),
|
|
|
|
Token::OpenSquiglyBracket => {}
|
|
|
|
_other => todo!(),
|
|
|
|
}
|
|
|
|
*i += 1;
|
|
|
|
}
|
|
|
|
argument_variables
|
2023-09-16 22:21:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ast(tokens: Vec<Token>) -> Vec<Function> {
|
|
|
|
let mut functions: Vec<Function> = Vec::new();
|
|
|
|
|
|
|
|
let mut i = 0;
|
|
|
|
while i < tokens.len() {
|
2023-09-17 21:52:14 +02:00
|
|
|
match &tokens[i] {
|
|
|
|
Token::Keyword(Keyword::Function) => {
|
2023-09-22 17:06:37 +02:00
|
|
|
i += 1;
|
|
|
|
|
|
|
|
let name = match &tokens[i] {
|
|
|
|
Token::Identifier(identifier) => identifier,
|
2023-09-17 21:52:14 +02:00
|
|
|
_ => todo!(),
|
2023-09-22 17:06:37 +02:00
|
|
|
}
|
|
|
|
.to_owned();
|
|
|
|
i += 1;
|
|
|
|
|
|
|
|
let arguments = parse_arguments(&tokens, &mut i);
|
|
|
|
|
|
|
|
i += 1;
|
|
|
|
let statements = parse_statements(&tokens, &mut i);
|
|
|
|
|
|
|
|
let statement = Statement::Scope(statements);
|
|
|
|
|
|
|
|
functions.push(Function::new(name, arguments, statement));
|
2023-09-16 22:21:53 +02:00
|
|
|
}
|
2023-09-17 21:52:14 +02:00
|
|
|
_other => {
|
|
|
|
dbg!(&functions);
|
|
|
|
dbg!(i);
|
|
|
|
dbg!(_other);
|
|
|
|
todo!();
|
2023-09-16 22:21:53 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
|
2023-09-17 21:52:14 +02:00
|
|
|
functions
|
2023-09-16 22:21:53 +02:00
|
|
|
}
|