begin new error system, unfinished
This commit is contained in:
parent
32c2a20697
commit
532fffe725
6 changed files with 673 additions and 237 deletions
266
src/parser.rs
266
src/parser.rs
|
@ -1,4 +1,6 @@
|
|||
use crate::lexer::{Token, TokenType};
|
||||
use crate::{lexer::{Token, TokenType}, Context};
|
||||
use std::process;
|
||||
use crate::errors::{create_error, print_error, ErrorSubType, ErrorType};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum ASTPart {
|
||||
|
@ -158,7 +160,7 @@ fn is_end(input: &Token, end: &Vec<Token>) -> bool {
|
|||
return false;
|
||||
}
|
||||
|
||||
fn read_call(variable: ASTPart, pos: &mut usize, input: &Vec<Token>) -> ASTPart {
|
||||
fn read_call(variable: ASTPart, pos: &mut usize, input: &Vec<Token>, ctx: &Context) -> ASTPart {
|
||||
let mut args: Vec<ASTPart> = vec![];
|
||||
*pos += 1;
|
||||
let start_pos = input[*pos-1].pos;
|
||||
|
@ -176,7 +178,7 @@ fn read_call(variable: ASTPart, pos: &mut usize, input: &Vec<Token>) -> ASTPart
|
|||
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);
|
||||
let arg = read_exp(pos, input, &ends, &ends, ctx);
|
||||
args.push(arg);
|
||||
}
|
||||
return ASTPart::Call(AstCall { function: Box::new(variable), args: args, pos: start_pos });
|
||||
|
@ -193,7 +195,7 @@ fn operator_precedence(op: &str) -> i64 {
|
|||
}
|
||||
}
|
||||
|
||||
fn shunt(input: Vec<ASTPart>) -> ASTPart {
|
||||
fn shunt(input: Vec<ASTPart>, ctx: &Context) -> ASTPart {
|
||||
let mut output: Vec<ASTPart> = vec![];
|
||||
let mut stack: Vec<ASTPart> = vec![];
|
||||
for part in input {
|
||||
|
@ -262,7 +264,9 @@ fn shunt(input: Vec<ASTPart>) -> ASTPart {
|
|||
if op.operator == "!" {
|
||||
println!("{:?}", output);
|
||||
if i < 1 {
|
||||
panic!("Unexpected operation at {}", op.pos);
|
||||
let err = create_error(&format!("Unexpected operation `{}`", op.operator), op.pos, ErrorType::SemanticError, ErrorSubType::UnexpectedOperation);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
}
|
||||
let left = output[i-1].clone();
|
||||
output[i] = ASTPart::Operation(AstOperation {
|
||||
|
@ -274,7 +278,9 @@ fn shunt(input: Vec<ASTPart>) -> ASTPart {
|
|||
output.remove(i-1);
|
||||
} else {
|
||||
if i < 2 {
|
||||
panic!("Unexpected operation at {}", op.pos);
|
||||
let err = create_error(&format!("Unexpected operation `{}`", op.operator), op.pos, ErrorType::SemanticError, ErrorSubType::UnexpectedOperation);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
}
|
||||
let left = output[i-2].clone();
|
||||
let right = output[i-1].clone();
|
||||
|
@ -296,12 +302,14 @@ fn shunt(input: Vec<ASTPart>) -> ASTPart {
|
|||
}
|
||||
return output[0].clone();
|
||||
}
|
||||
fn read_function(input: &Vec<Token>, pos: &mut usize, with_args: bool) -> ASTPart {
|
||||
fn read_function(input: &Vec<Token>, pos: &mut usize, with_args: bool, ctx: &Context) -> ASTPart {
|
||||
let mut args: Vec<String> = vec![];
|
||||
let start_pos = input[*pos].pos;
|
||||
if with_args {
|
||||
if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != "(" {
|
||||
panic!("Expected ( at {}", input[*pos].pos);
|
||||
let err = create_error(&format!("Expected `(`"), input[*pos].pos, ErrorType::SyntaxError, ErrorSubType::Expected);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
}
|
||||
*pos += 1;
|
||||
while pos < &mut input.len() {
|
||||
|
@ -316,16 +324,22 @@ fn read_function(input: &Vec<Token>, pos: &mut usize, with_args: bool) -> ASTPar
|
|||
if token.typ == TokenType::IDENTIFIER {
|
||||
args.push(token.value.clone());
|
||||
} else {
|
||||
panic!("Unexpected {:?}({}) at {}", token.typ, token.value, token.pos);
|
||||
let err = create_error(&format!("Unexpected `{:?}({})`", token.typ, token.value), token.pos, ErrorType::SyntaxError, ErrorSubType::Unexpected);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != ")" {
|
||||
panic!("Unexpected end of arguments at {}", input[*pos].pos);
|
||||
let err = create_error(&format!("Unexpected end of arguments"), input[*pos].pos, ErrorType::SyntaxError, ErrorSubType::UnexpectedEnd);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
}
|
||||
*pos += 1;
|
||||
}
|
||||
if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != "{" {
|
||||
panic!("Expected {{ at {}", input[*pos].pos);
|
||||
let err = create_error(&format!("Expected {{"), input[*pos].pos, ErrorType::SyntaxError, ErrorSubType::Expected);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
}
|
||||
*pos += 1;
|
||||
let op_ends: Vec<Token> = vec![
|
||||
|
@ -336,15 +350,17 @@ fn read_function(input: &Vec<Token>, pos: &mut usize, with_args: bool) -> ASTPar
|
|||
let parse_ends: Vec<Token> = vec![
|
||||
Token { typ: TokenType::SEPARATOR, value: String::from("}"), pos: 0 }
|
||||
];
|
||||
let body = parse_internal(input, &op_ends, &parse_ends, pos);
|
||||
let body = parse_internal(input, &op_ends, &parse_ends, pos, ctx);
|
||||
if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != "}" {
|
||||
panic!("Unexpected end of function at {}", input[*pos].pos);
|
||||
let err = create_error(&format!("Unexpected end of function"), input[*pos].pos, ErrorType::SyntaxError, ErrorSubType::UnexpectedEnd);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
}
|
||||
*pos += 1;
|
||||
return ASTPart::Function(AstFunction { args: args, body: body, pos: start_pos });
|
||||
}
|
||||
|
||||
fn read_table(input: &Vec<Token>, pos: &mut usize) -> ASTPart {
|
||||
fn read_table(input: &Vec<Token>, pos: &mut usize, ctx: &Context) -> ASTPart {
|
||||
let start_pos = input[*pos].pos;
|
||||
let key_ends: Vec<Token> = vec![
|
||||
Token { typ: TokenType::SEPARATOR, value: String::from("]"), pos: 0 }
|
||||
|
@ -358,28 +374,36 @@ fn read_table(input: &Vec<Token>, pos: &mut usize) -> ASTPart {
|
|||
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);
|
||||
let keyy = read_exp(pos, input, &key_ends, &key_ends, ctx);
|
||||
match keyy {
|
||||
ASTPart::Table(_) => {
|
||||
panic!("Table keys cannot be tables at {}", input[*pos].pos);
|
||||
let err = create_error(&format!("Table keys cannot be tables"), input[*pos].pos, ErrorType::SemanticError, ErrorSubType::InvalidTableKeys);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
},
|
||||
ASTPart::Function(_) => {
|
||||
panic!("Table keys cannot be functions at {}", input[*pos].pos);
|
||||
let err = create_error(&format!("Table keys cannot be functions"), input[*pos].pos, ErrorType::SemanticError, ErrorSubType::InvalidTableKeys);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != "]" {
|
||||
panic!("Unexpected end of key at {}", input[*pos].pos);
|
||||
let err = create_error(&format!("Unexpected end of key"), input[*pos].pos, ErrorType::SyntaxError, ErrorSubType::UnexpectedEnd);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
}
|
||||
*pos += 1;
|
||||
if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != "=" {
|
||||
panic!("Expected = after key at {}", input[*pos].pos);
|
||||
let err = create_error(&format!("Expected `=` after key"), input[*pos].pos, ErrorType::SyntaxError, ErrorSubType::Expected);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
}
|
||||
*pos += 1;
|
||||
let value = read_exp(pos, input, &ends, &ends);
|
||||
let value = read_exp(pos, input, &ends, &ends, ctx);
|
||||
tbl.push(TableValue { key: keyy, value: value, pos: input[*pos].pos });
|
||||
} else {
|
||||
let value = read_exp(pos, input, &ends, &ends);
|
||||
let value = read_exp(pos, input, &ends, &ends, ctx);
|
||||
tbl.push(TableValue { key: ASTPart::Number(AstNumber { value: key, pos: 0 }), value: value, pos: input[*pos].pos });
|
||||
key += 1;
|
||||
}
|
||||
|
@ -390,13 +414,15 @@ fn read_table(input: &Vec<Token>, pos: &mut usize) -> ASTPart {
|
|||
*pos += 1;
|
||||
continue;
|
||||
} else {
|
||||
panic!("Unexpected end of table at {}", input[*pos].pos);
|
||||
let err = create_error(&format!("Unexpected end of table"), input[*pos].pos, ErrorType::SyntaxError, ErrorSubType::UnexpectedEnd);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
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>, ctx: &Context) -> ASTPart {
|
||||
let mut expressions: Vec<ASTPart> = vec![];
|
||||
while pos < &mut input.len() {
|
||||
let token = &input[*pos];
|
||||
|
@ -427,33 +453,41 @@ fn read_exp(pos: &mut usize, input: &Vec<Token>, ends: &Vec<Token>, parse_ends:
|
|||
} else if token.value == "nincs hám" {
|
||||
expressions.push(ASTPart::Null(AstNull { pos: token.pos }));
|
||||
} else if token.value == "lőcsve" {
|
||||
let func = read_function(input, pos, true);
|
||||
let func = read_function(input, pos, true, ctx);
|
||||
expressions.push(func);
|
||||
} else {
|
||||
panic!("Unexpected {:?}({}) at {}", token.typ, token.value, token.pos);
|
||||
let err = create_error(&format!("Unexpected `{:?}({})`", token.typ, token.value), token.pos, ErrorType::SyntaxError, ErrorSubType::Unexpected);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
}
|
||||
} 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));
|
||||
expressions.push(read_call(var, pos, input, ctx));
|
||||
} else if next_token.typ == TokenType::SEPARATOR && next_token.value == "[" {
|
||||
let var_pos = token.pos;
|
||||
let key_ends: Vec<Token> = vec![
|
||||
Token { typ: TokenType::SEPARATOR, value: String::from("]"), pos: 0 }
|
||||
];
|
||||
*pos += 1;
|
||||
let keyy = read_exp(pos, input, &key_ends, &key_ends);
|
||||
let keyy = read_exp(pos, input, &key_ends, &key_ends, ctx);
|
||||
match keyy {
|
||||
ASTPart::Table(_) => {
|
||||
panic!("Table keys cannot be tables at {}", input[*pos].pos);
|
||||
let err = create_error(&format!("Table keys cannot be tables"), input[*pos].pos, ErrorType::SemanticError, ErrorSubType::InvalidTableKeys);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
},
|
||||
ASTPart::Function(_) => {
|
||||
panic!("Table keys cannot be functions at {}", input[*pos].pos);
|
||||
let err = create_error(&format!("Table keys cannot be functions"), input[*pos].pos, ErrorType::SemanticError, ErrorSubType::InvalidTableKeys);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != "]" {
|
||||
panic!("Unexpected end of key at {}", input[*pos].pos);
|
||||
let err = create_error(&format!("Unexpected end of key"), input[*pos].pos, ErrorType::SyntaxError, ErrorSubType::UnexpectedEnd);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
}
|
||||
*pos += 1;
|
||||
expressions.push(ASTPart::TableGet(AstTableGet { table: Box::new(ASTPart::VarRead(AstVarRead { variable: token.value.clone(), pos: var_pos })), key: Box::new(keyy), pos: var_pos+1 }));
|
||||
|
@ -468,28 +502,34 @@ fn read_exp(pos: &mut usize, input: &Vec<Token>, ends: &Vec<Token>, parse_ends:
|
|||
let ends: Vec<Token> = vec![
|
||||
Token { typ: TokenType::SEPARATOR, value: String::from(")"), pos: 0 }
|
||||
];
|
||||
let exp = read_exp(pos, input, &ends, &ends);
|
||||
let exp = read_exp(pos, input, &ends, &ends, ctx);
|
||||
if input[*pos].typ == TokenType::SEPARATOR && input[*pos].value == ")" {
|
||||
*pos += 1;
|
||||
} else {
|
||||
panic!("Unclosed parenthesis at {}", token.pos);
|
||||
let err = create_error(&format!("Unclosed parenthesis"), input[*pos].pos, ErrorType::SyntaxError, ErrorSubType::Unclosed);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
}
|
||||
expressions.push(exp);
|
||||
} else if token.value == "{" {
|
||||
let tbl = read_table(input, pos);
|
||||
let tbl = read_table(input, pos, ctx);
|
||||
expressions.push(tbl);
|
||||
} else {
|
||||
panic!("Unexpected {:?}({}) at {}", token.typ, token.value, token.pos);
|
||||
let err = create_error(&format!("Unexpected `{:?}({})`", token.typ, token.value), token.pos, ErrorType::SyntaxError, ErrorSubType::Unexpected);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
}
|
||||
} else {
|
||||
panic!("Unexpected {:?}({}) at {}", token.typ, token.value, token.pos);
|
||||
let err = create_error(&format!("Unexpected `{:?}({})`", token.typ, token.value), token.pos, ErrorType::SyntaxError, ErrorSubType::Unexpected);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
let shunted = shunt(expressions);
|
||||
let shunted = shunt(expressions, ctx);
|
||||
return shunted;
|
||||
}
|
||||
|
||||
fn next_operation(pos: &mut usize, input: &Vec<Token>, op_ends: &Vec<Token>, parse_ends: &Vec<Token>) -> ASTPart {
|
||||
fn next_operation(pos: &mut usize, input: &Vec<Token>, op_ends: &Vec<Token>, parse_ends: &Vec<Token>, ctx: &Context) -> ASTPart {
|
||||
let token = &input[*pos];
|
||||
let mut next_token = &Token {
|
||||
typ: TokenType::OPEND,
|
||||
|
@ -511,173 +551,233 @@ fn next_operation(pos: &mut usize, input: &Vec<Token>, op_ends: &Vec<Token>, par
|
|||
let variable = &input[*pos];
|
||||
*pos += 1;
|
||||
if variable.typ != TokenType::IDENTIFIER {
|
||||
panic!("Unexpected {:?} at {}", variable.typ, variable.pos)
|
||||
let err = create_error(&format!("Unexpected `{:?}`", variable.typ,), token.pos, ErrorType::SyntaxError, ErrorSubType::Unexpected);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
}
|
||||
let eq = &input[*pos];
|
||||
if eq.typ == TokenType::SEPARATOR && eq.value == "=" {
|
||||
*pos += 1;
|
||||
}
|
||||
let value = read_exp(pos, input, op_ends, parse_ends);
|
||||
let value = read_exp(pos, input, op_ends, parse_ends, ctx);
|
||||
return ASTPart::Assigment(AstAssigment { variable: variable.value.clone(), value: Box::new(value), pos: token.pos });
|
||||
} else if token.value == "ha geny" {
|
||||
if next_token.typ != TokenType::SEPARATOR || next_token.value != "(" {
|
||||
panic!("Expected ( at {}", token.pos);
|
||||
let err = create_error(&format!("Expected `(`"), token.pos, ErrorType::SyntaxError, ErrorSubType::Expected);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
}
|
||||
*pos += 1;
|
||||
let condition_end = vec![
|
||||
Token { typ: TokenType::SEPARATOR, value: String::from(")"), pos: 0 }
|
||||
];
|
||||
let condition = read_exp(pos, input, &condition_end, &condition_end);
|
||||
let condition = read_exp(pos, input, &condition_end, &condition_end, ctx);
|
||||
if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != ")" {
|
||||
panic!("Unexpected end of condition at {}", token.pos);
|
||||
let err = create_error(&format!("Unexpected end of condition"), token.pos, ErrorType::SyntaxError, ErrorSubType::UnexpectedEnd);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
}
|
||||
*pos += 1;
|
||||
let body = read_function(input, pos, false);
|
||||
let body = read_function(input, pos, false, ctx);
|
||||
let real_body = match body {
|
||||
ASTPart::Function(func) => func.body,
|
||||
_ => panic!("Expected function body at {}", token.pos)
|
||||
_ => {
|
||||
let err = create_error(&format!("Expected function body"), token.pos, ErrorType::SyntaxError, ErrorSubType::Expected);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
}
|
||||
};
|
||||
return ASTPart::If(AstIf { condition: Box::new(condition), body: real_body, pos: token.pos });
|
||||
} else if token.value == "amíg geny" {
|
||||
if next_token.typ != TokenType::SEPARATOR || next_token.value != "(" {
|
||||
panic!("Expected ( at {}", token.pos);
|
||||
let err = create_error(&format!("Expected `(`"), token.pos, ErrorType::SyntaxError, ErrorSubType::Expected);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
}
|
||||
*pos += 1;
|
||||
let condition_end = vec![
|
||||
Token { typ: TokenType::SEPARATOR, value: String::from(")"), pos: 0 }
|
||||
];
|
||||
let condition = read_exp(pos, input, &condition_end, &condition_end);
|
||||
let condition = read_exp(pos, input, &condition_end, &condition_end, ctx);
|
||||
if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != ")" {
|
||||
panic!("Unexpected end of condition at {}", token.pos);
|
||||
let err = create_error(&format!("Unexpected end of condition"), token.pos, ErrorType::SyntaxError, ErrorSubType::UnexpectedEnd);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
}
|
||||
*pos += 1;
|
||||
let body = read_function(input, pos, false);
|
||||
let body = read_function(input, pos, false, ctx);
|
||||
let real_body = match body {
|
||||
ASTPart::Function(func) => func.body,
|
||||
_ => panic!("Expected function body at {}", token.pos)
|
||||
_ => {
|
||||
let err = create_error(&format!("Expected function body"), token.pos, ErrorType::SyntaxError, ErrorSubType::Expected);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
}
|
||||
};
|
||||
return ASTPart::While(AstWhile { condition: Box::new(condition), body: real_body, pos: token.pos });
|
||||
} else if token.value == "kraf" {
|
||||
return ASTPart::Break(AstBreak { pos: token.pos });
|
||||
} else if token.value == "kopva" {
|
||||
if next_token.typ != TokenType::SEPARATOR || next_token.value != "(" {
|
||||
panic!("Expected ( at {}", token.pos);
|
||||
let err = create_error(&format!("Expected `(`"), token.pos, ErrorType::SyntaxError, ErrorSubType::Expected);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
}
|
||||
*pos += 1;
|
||||
let ends = vec![
|
||||
Token { typ: TokenType::SEPARATOR, value: String::from(")"), pos: 0 },
|
||||
Token { typ: TokenType::OPEND, value: String::from(";"), pos: 0 }
|
||||
];
|
||||
let init = parse_internal(input, &ends, &ends, pos);
|
||||
let init = parse_internal(input, &ends, &ends, pos, ctx);
|
||||
if init.len() != 1 {
|
||||
panic!("Only one expression is expected for init at {}", token.pos);
|
||||
let err = create_error(&format!("Only one expression is expected for init"), token.pos, ErrorType::SyntaxError, ErrorSubType::Expected);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
}
|
||||
if input[*pos].typ != TokenType::OPEND || input[*pos].value != ";" {
|
||||
panic!("Unexpected end of init at {}", token.pos);
|
||||
let err = create_error(&format!("Unexpected end of init"), token.pos, ErrorType::SyntaxError, ErrorSubType::UnexpectedEnd);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
}
|
||||
*pos += 1;
|
||||
let condition = read_exp(pos, input, &ends, &ends);
|
||||
let condition = read_exp(pos, input, &ends, &ends, ctx);
|
||||
if input[*pos].typ != TokenType::OPEND || input[*pos].value != ";" {
|
||||
panic!("Unexpected end of condition at {}", token.pos);
|
||||
let err = create_error(&format!("Unexpected end of condition"), token.pos, ErrorType::SyntaxError, ErrorSubType::UnexpectedEnd);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
}
|
||||
*pos += 1;
|
||||
let update = parse_internal(input, &ends, &ends, pos);
|
||||
let update = parse_internal(input, &ends, &ends, pos, ctx);
|
||||
if update.len() != 1 {
|
||||
panic!("Only one expression is expected for update at {}", token.pos);
|
||||
let err = create_error(&format!("Only one expression is expected for update"), token.pos, ErrorType::SyntaxError, ErrorSubType::Expected);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
}
|
||||
if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != ")" {
|
||||
panic!("Unexpected end of update at {}", token.pos);
|
||||
let err = create_error(&format!("Unexpected end of update"), token.pos, ErrorType::SyntaxError, ErrorSubType::UnexpectedEnd);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
}
|
||||
*pos += 1;
|
||||
let body = read_function(input, pos, false);
|
||||
let body = read_function(input, pos, false, ctx);
|
||||
let real_body = match body {
|
||||
ASTPart::Function(func) => func.body,
|
||||
_ => panic!("Expected function body at {}", token.pos)
|
||||
_ => {
|
||||
let err = create_error(&format!("Expected function body"), token.pos, ErrorType::SyntaxError, ErrorSubType::Expected);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
}
|
||||
};
|
||||
return ASTPart::For(AstFor { init: Box::new(init[0].clone()), condition: Box::new(condition), update: Box::new(update[0].clone()), body: real_body, pos: token.pos });
|
||||
} else if token.value == "szard le" {
|
||||
return ASTPart::Continue(AstContinue { pos: token.pos });
|
||||
} else if token.value == "ha nem geny akkor geny" {
|
||||
if next_token.typ != TokenType::SEPARATOR || next_token.value != "(" {
|
||||
panic!("Expected ( at {}", token.pos);
|
||||
let err = create_error(&format!("Expected `(`"), token.pos, ErrorType::SyntaxError, ErrorSubType::Expected);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
}
|
||||
*pos += 1;
|
||||
let condition_end = vec![
|
||||
Token { typ: TokenType::SEPARATOR, value: String::from(")"), pos: 0 }
|
||||
];
|
||||
let condition = read_exp(pos, input, &condition_end, &condition_end);
|
||||
let condition = read_exp(pos, input, &condition_end, &condition_end, ctx);
|
||||
if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != ")" {
|
||||
panic!("Unexpected end of condition at {}", token.pos);
|
||||
let err = create_error(&format!("Unexpected end of condition"), token.pos, ErrorType::SyntaxError, ErrorSubType::UnexpectedEnd);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
}
|
||||
*pos += 1;
|
||||
let body = read_function(input, pos, false);
|
||||
let body = read_function(input, pos, false, ctx);
|
||||
let real_body = match body {
|
||||
ASTPart::Function(func) => func.body,
|
||||
_ => panic!("Expected function body at {}", token.pos)
|
||||
_ => {
|
||||
let err = create_error(&format!("Expected function body"), token.pos, ErrorType::SyntaxError, ErrorSubType::Expected);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
}
|
||||
};
|
||||
return ASTPart::ElseIf(AstElseIf { condition: Box::new(condition), body: real_body, pos: token.pos });
|
||||
} else if token.value == "ha nem geny" {
|
||||
let body = read_function(input, pos, false);
|
||||
let body = read_function(input, pos, false, ctx);
|
||||
let real_body = match body {
|
||||
ASTPart::Function(func) => func.body,
|
||||
_ => panic!("Expected function body at {}", token.pos)
|
||||
_ => {
|
||||
let err = create_error(&format!("Expected function body"), token.pos, ErrorType::SyntaxError, ErrorSubType::Expected);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
}
|
||||
};
|
||||
return ASTPart::Else(AstElse { body: real_body, pos: token.pos });
|
||||
} else if token.value == "reti" {
|
||||
if is_end(next_token, op_ends) || is_end(next_token, parse_ends) {
|
||||
return ASTPart::Return(AstReturn { value: Box::new(ASTPart::Null(AstNull { pos: token.pos })), pos: token.pos });
|
||||
} else {
|
||||
let value = read_exp(pos, input, op_ends, parse_ends);
|
||||
let value = read_exp(pos, input, op_ends, parse_ends, ctx);
|
||||
return ASTPart::Return(AstReturn { value: Box::new(value), pos: token.pos });
|
||||
}
|
||||
} else {
|
||||
panic!("Unexpected {:?}({}) at {}", token.typ, token.value, token.pos);
|
||||
let err = create_error(&format!("Unexpected `{:?}({})`", token.typ, token.value), token.pos, ErrorType::SyntaxError, ErrorSubType::Unexpected);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
}
|
||||
} 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 });
|
||||
return read_call(var, pos, input);
|
||||
return read_call(var, pos, input, ctx);
|
||||
} else if next_token.typ == TokenType::SEPARATOR && next_token.value == "[" {
|
||||
*pos += 1;
|
||||
let key_ends: Vec<Token> = vec![
|
||||
Token { typ: TokenType::SEPARATOR, value: String::from("]"), pos: 0 }
|
||||
];
|
||||
let keyy = read_exp(pos, input, &key_ends, &key_ends);
|
||||
let keyy = read_exp(pos, input, &key_ends, &key_ends, ctx);
|
||||
match keyy {
|
||||
ASTPart::Table(_) => {
|
||||
panic!("Table keys cannot be tables at {}", input[*pos].pos);
|
||||
let err = create_error(&format!("Table keys cannot be tables"), input[*pos].pos, ErrorType::SemanticError, ErrorSubType::InvalidTableKeys);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
},
|
||||
ASTPart::Function(_) => {
|
||||
panic!("Table keys cannot be functions at {}", input[*pos].pos);
|
||||
let err = create_error(&format!("Table keys cannot be functions"), input[*pos].pos, ErrorType::SemanticError, ErrorSubType::InvalidTableKeys);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != "]" {
|
||||
panic!("Unexpected end of key at {}", input[*pos].pos);
|
||||
let err = create_error(&format!("Unexpected end of key"), input[*pos].pos, ErrorType::SyntaxError, ErrorSubType::UnexpectedEnd);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
}
|
||||
*pos += 1;
|
||||
if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != "=" {
|
||||
panic!("Expected = after key at {}", input[*pos].pos);
|
||||
let err = create_error(&format!("Expected `=` after key"), input[*pos].pos, ErrorType::SyntaxError, ErrorSubType::Expected);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
}
|
||||
*pos += 1;
|
||||
let value = read_exp(pos, input, op_ends, parse_ends);
|
||||
let value = read_exp(pos, input, op_ends, parse_ends, ctx);
|
||||
return ASTPart::TableSet(AstTableSet { table: Box::new(ASTPart::VarRead(AstVarRead { variable: token.value.clone(), pos: token.pos })), key: Box::new(keyy), value: Box::new(value), pos: token.pos });
|
||||
} else if next_token.typ == TokenType::SEPARATOR && next_token.value == "=" {
|
||||
*pos += 1;
|
||||
let value = read_exp(pos, input, op_ends, parse_ends);
|
||||
let value = read_exp(pos, input, op_ends, parse_ends, ctx);
|
||||
return ASTPart::VarUpdate(AstVarUpdate { variable: token.value.clone(), value: Box::new(value), pos: token.pos });
|
||||
} else {
|
||||
panic!("Unexpected {:?}({}) at {}", token.typ, token.value, token.pos);
|
||||
let err = create_error(&format!("Unexpected `{:?}({})`", token.typ, token.value), token.pos, ErrorType::SyntaxError, ErrorSubType::Unexpected);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
}
|
||||
} else {
|
||||
panic!("Unexpected {:?}({}) at {}", token.typ, token.value, token.pos);
|
||||
let err = create_error(&format!("Unexpected `{:?}({})`", token.typ, token.value), token.pos, ErrorType::SyntaxError, ErrorSubType::Unexpected);
|
||||
print_error(&err, &ctx);
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_internal(input: &Vec<Token>, op_ends: &Vec<Token>, parse_ends: &Vec<Token>, pos: &mut usize) -> Vec<ASTPart> {
|
||||
fn parse_internal(input: &Vec<Token>, op_ends: &Vec<Token>, parse_ends: &Vec<Token>, pos: &mut usize, ctx: &Context) -> Vec<ASTPart> {
|
||||
let mut out: Vec<ASTPart> = vec![];
|
||||
while *pos < input.len() {
|
||||
let op = next_operation(pos, &input, &op_ends, &parse_ends);
|
||||
let op = next_operation(pos, &input, &op_ends, &parse_ends, ctx);
|
||||
match op {
|
||||
ASTPart::NOOP => {},
|
||||
_ => {
|
||||
|
@ -690,7 +790,7 @@ fn parse_internal(input: &Vec<Token>, op_ends: &Vec<Token>, parse_ends: &Vec<Tok
|
|||
}
|
||||
return out;
|
||||
}
|
||||
pub fn parse(input: Vec<Token>) -> Vec<ASTPart> {
|
||||
pub fn parse(input: Vec<Token>, ctx: &Context) -> 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 },
|
||||
|
@ -699,6 +799,6 @@ pub fn parse(input: Vec<Token>) -> Vec<ASTPart> {
|
|||
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, &mut 0);
|
||||
let out = parse_internal(&input, &op_ends, &parse_ends, &mut 0, ctx);
|
||||
return out;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue