added tables to the parser
This commit is contained in:
parent
d1483f9e61
commit
6caafe77d0
3 changed files with 73 additions and 9 deletions
|
@ -104,7 +104,16 @@ fn log_ast_part(part: &ASTPart, prefix: String) {
|
||||||
println!("{}{}: Return", prefix, ret.pos);
|
println!("{}{}: Return", prefix, ret.pos);
|
||||||
println!("{} Value:", prefix);
|
println!("{} Value:", prefix);
|
||||||
log_ast_part(&ret.value, format!("{} ", prefix));
|
log_ast_part(&ret.value, format!("{} ", prefix));
|
||||||
|
},
|
||||||
|
ASTPart::Table(tbl) => {
|
||||||
|
println!("{}{}: Table:", prefix, tbl.pos);
|
||||||
|
for table_value in &tbl.values {
|
||||||
|
println!("{} Key:", prefix);
|
||||||
|
log_ast_part(&table_value.key, format!("{} ", prefix));
|
||||||
|
println!("{} Value:", prefix);
|
||||||
|
log_ast_part(&table_value.value, format!("{} ", prefix));
|
||||||
}
|
}
|
||||||
|
},
|
||||||
ASTPart::NOOP => println!("{}NOOP", prefix)
|
ASTPart::NOOP => println!("{}NOOP", prefix)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
use core::panic;
|
|
||||||
|
|
||||||
use crate::lexer::{Token, TokenType};
|
use crate::lexer::{Token, TokenType};
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
@ -22,6 +20,7 @@ pub enum ASTPart {
|
||||||
For(AstFor),
|
For(AstFor),
|
||||||
Continue(AstContinue),
|
Continue(AstContinue),
|
||||||
Return(AstReturn),
|
Return(AstReturn),
|
||||||
|
Table(AstTable),
|
||||||
NOOP
|
NOOP
|
||||||
}
|
}
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
@ -123,6 +122,17 @@ pub struct AstReturn {
|
||||||
pub value: Box<ASTPart>,
|
pub value: Box<ASTPart>,
|
||||||
pub pos: usize
|
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 {
|
fn is_end(input: &Token, end: &Vec<Token>) -> bool {
|
||||||
for token in end {
|
for token in end {
|
||||||
|
@ -194,6 +204,9 @@ fn shunt(input: Vec<ASTPart>) -> ASTPart {
|
||||||
ASTPart::Function(_) => {
|
ASTPart::Function(_) => {
|
||||||
output.push(part);
|
output.push(part);
|
||||||
},
|
},
|
||||||
|
ASTPart::Table(_) => {
|
||||||
|
output.push(part);
|
||||||
|
},
|
||||||
ASTPart::Operation(op) => {
|
ASTPart::Operation(op) => {
|
||||||
if *op.left != ASTPart::NOOP && *op.right != ASTPart::NOOP {
|
if *op.left != ASTPart::NOOP && *op.right != ASTPart::NOOP {
|
||||||
output.push(part);
|
output.push(part);
|
||||||
|
@ -312,6 +325,50 @@ fn read_function(input: &Vec<Token>, pos: &mut usize, with_args: bool) -> ASTPar
|
||||||
*pos += 1;
|
*pos += 1;
|
||||||
return ASTPart::Function(AstFunction { args: args, body: body, pos: start_pos });
|
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 {
|
fn read_exp(pos: &mut usize, input: &Vec<Token>, ends: &Vec<Token>, parse_ends: &Vec<Token>) -> ASTPart {
|
||||||
let mut expressions: Vec<ASTPart> = vec![];
|
let mut expressions: Vec<ASTPart> = vec![];
|
||||||
while pos < &mut input.len() {
|
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);
|
panic!("Unclosed parenthesis at {}", token.pos);
|
||||||
}
|
}
|
||||||
expressions.push(exp);
|
expressions.push(exp);
|
||||||
|
} else if token.value == "{" {
|
||||||
|
let tbl = read_table(input, pos);
|
||||||
|
expressions.push(tbl);
|
||||||
} else {
|
} else {
|
||||||
panic!("Unexpected {:?}({}) at {}", token.typ, token.value, token.pos);
|
panic!("Unexpected {:?}({}) at {}", token.typ, token.value, token.pos);
|
||||||
}
|
}
|
||||||
|
|
7
test.as
7
test.as
|
@ -1,6 +1 @@
|
||||||
gethelj a = 1
|
gethelj a = {1,2,3}
|
||||||
gethelj b = lőcsve() {
|
|
||||||
gethelj a = 2
|
|
||||||
reti 6
|
|
||||||
}
|
|
||||||
gethelj c = b()
|
|
Loading…
Add table
Add a link
Reference in a new issue