added tables to the parser

This commit is contained in:
afonya2 2025-06-03 19:01:13 +02:00
parent d1483f9e61
commit 6caafe77d0
Signed by: afonya
GPG key ID: EBB9C4CAFAAFB2DC
3 changed files with 73 additions and 9 deletions

View file

@ -1,5 +1,3 @@
use core::panic;
use crate::lexer::{Token, TokenType};
#[derive(Debug, Clone, PartialEq)]
@ -22,6 +20,7 @@ pub enum ASTPart {
For(AstFor),
Continue(AstContinue),
Return(AstReturn),
Table(AstTable),
NOOP
}
#[derive(Debug, Clone, PartialEq)]
@ -123,6 +122,17 @@ pub struct AstReturn {
pub value: Box<ASTPart>,
pub pos: usize
}
#[derive(Debug, Clone, PartialEq)]
pub struct AstTable {
pub values: Vec<TableValue>,
pub pos: usize
}
#[derive(Debug, Clone, PartialEq)]
pub struct TableValue {
pub key: ASTPart,
pub value: ASTPart,
pub pos: usize
}
fn is_end(input: &Token, end: &Vec<Token>) -> bool {
for token in end {
@ -194,6 +204,9 @@ fn shunt(input: Vec<ASTPart>) -> ASTPart {
ASTPart::Function(_) => {
output.push(part);
},
ASTPart::Table(_) => {
output.push(part);
},
ASTPart::Operation(op) => {
if *op.left != ASTPart::NOOP && *op.right != ASTPart::NOOP {
output.push(part);
@ -312,6 +325,50 @@ fn read_function(input: &Vec<Token>, pos: &mut usize, with_args: bool) -> ASTPar
*pos += 1;
return ASTPart::Function(AstFunction { args: args, body: body, pos: start_pos });
}
fn read_table(input: &Vec<Token>, pos: &mut usize) -> ASTPart {
let start_pos = input[*pos].pos;
let key_ends: Vec<Token> = vec![
Token { typ: TokenType::SEPARATOR, value: String::from("]"), pos: 0 }
];
let ends: Vec<Token> = vec![
Token { typ: TokenType::SEPARATOR, value: String::from("}"), pos: 0 },
Token { typ: TokenType::SEPARATOR, value: String::from(","), pos: 0 }
];
let mut tbl: Vec<TableValue> = vec![];
let mut key = 0;
while *pos < input.len() {
if input[*pos].typ == TokenType::SEPARATOR && input[*pos].value == "[" {
*pos += 1;
let keyy = read_exp(pos, input, &key_ends, &key_ends);
if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != "]" {
panic!("Unexpected end of key at {}", input[*pos].pos);
}
*pos += 1;
if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != "=" {
panic!("Expected = after key at {}", input[*pos].pos);
}
*pos += 1;
let value = read_exp(pos, input, &ends, &ends);
tbl.push(TableValue { key: keyy, value: value, pos: input[*pos].pos });
} else {
let value = read_exp(pos, input, &ends, &ends);
tbl.push(TableValue { key: ASTPart::Number(AstNumber { value: key, pos: 0 }), value: value, pos: input[*pos].pos });
key += 1;
}
if input[*pos].typ == TokenType::SEPARATOR && input[*pos].value == "}" {
*pos += 1;
break;
} else if input[*pos].typ == TokenType::SEPARATOR && input[*pos].value == "," {
*pos += 1;
continue;
} else {
panic!("Unexpected end of table at {}", input[*pos].pos);
}
}
return ASTPart::Table(AstTable { values: tbl, pos: start_pos });
}
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() {
@ -370,6 +427,9 @@ fn read_exp(pos: &mut usize, input: &Vec<Token>, ends: &Vec<Token>, parse_ends:
panic!("Unclosed parenthesis at {}", token.pos);
}
expressions.push(exp);
} else if token.value == "{" {
let tbl = read_table(input, pos);
expressions.push(tbl);
} else {
panic!("Unexpected {:?}({}) at {}", token.typ, token.value, token.pos);
}