Cleaner unop parsing
This commit is contained in:
parent
235eb460dc
commit
7ea5f67f9c
40
src/main.rs
40
src/main.rs
@ -1,7 +1,8 @@
|
|||||||
use std::{
|
use std::{
|
||||||
env::args,
|
env::args,
|
||||||
fs,
|
fs,
|
||||||
io::{stdin, stdout, Write}, process::exit,
|
// io::{stdin, stdout, Write},
|
||||||
|
process::exit,
|
||||||
};
|
};
|
||||||
|
|
||||||
use nek_lang::interpreter::Interpreter;
|
use nek_lang::interpreter::Interpreter;
|
||||||
@ -11,7 +12,7 @@ struct CliConfig {
|
|||||||
print_tokens: bool,
|
print_tokens: bool,
|
||||||
print_ast: bool,
|
print_ast: bool,
|
||||||
no_optimizations: bool,
|
no_optimizations: bool,
|
||||||
interactive: bool,
|
// interactive: bool,
|
||||||
file: Option<String>,
|
file: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -24,7 +25,7 @@ fn main() {
|
|||||||
"--token" | "-t" => conf.print_tokens = true,
|
"--token" | "-t" => conf.print_tokens = true,
|
||||||
"--ast" | "-a" => conf.print_ast = true,
|
"--ast" | "-a" => conf.print_ast = true,
|
||||||
"--no-opt" | "-n" => conf.no_optimizations = true,
|
"--no-opt" | "-n" => conf.no_optimizations = true,
|
||||||
"--interactive" | "-i" => conf.interactive = true,
|
// "--interactive" | "-i" => conf.interactive = true,
|
||||||
"--help" | "-h" => print_help(),
|
"--help" | "-h" => print_help(),
|
||||||
file if conf.file.is_none() => conf.file = Some(file.to_string()),
|
file if conf.file.is_none() => conf.file = Some(file.to_string()),
|
||||||
_ => panic!("Invalid argument: '{}'", arg),
|
_ => panic!("Invalid argument: '{}'", arg),
|
||||||
@ -42,23 +43,28 @@ fn main() {
|
|||||||
interpreter.run_str(&code);
|
interpreter.run_str(&code);
|
||||||
}
|
}
|
||||||
|
|
||||||
if conf.interactive || conf.file.is_none() {
|
// TODO: The interactive prompt is currently broken due to the precalculated stack positions.
|
||||||
let mut code = String::new();
|
// For this to still work, there needs to be a way to keep the stack in the interpreter after
|
||||||
|
// runing once. Also somehow the stringstore and var stack from the last parsing stages would
|
||||||
|
// need to be reused for the parser to work correctly
|
||||||
|
|
||||||
loop {
|
// if conf.interactive || conf.file.is_none() {
|
||||||
print!(">> ");
|
// let mut code = String::new();
|
||||||
stdout().flush().unwrap();
|
|
||||||
|
|
||||||
code.clear();
|
// loop {
|
||||||
stdin().read_line(&mut code).unwrap();
|
// print!(">> ");
|
||||||
|
// stdout().flush().unwrap();
|
||||||
|
|
||||||
if code.trim() == "exit" {
|
// code.clear();
|
||||||
break;
|
// stdin().read_line(&mut code).unwrap();
|
||||||
}
|
|
||||||
|
|
||||||
interpreter.run_str(&code);
|
// if code.trim() == "exit" {
|
||||||
}
|
// break;
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
// interpreter.run_str(&code);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_help() {
|
fn print_help() {
|
||||||
@ -67,7 +73,7 @@ fn print_help() {
|
|||||||
println!("-t, --token Print the lexed tokens");
|
println!("-t, --token Print the lexed tokens");
|
||||||
println!("-a, --ast Print the abstract syntax tree");
|
println!("-a, --ast Print the abstract syntax tree");
|
||||||
println!("-n, --no-opt Disable the AST optimizations");
|
println!("-n, --no-opt Disable the AST optimizations");
|
||||||
println!("-i, --interactive Interactive mode");
|
// println!("-i, --interactive Interactive mode");
|
||||||
println!("-h, --help Show this help screen");
|
println!("-h, --help Show this help screen");
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,7 +2,7 @@ use std::iter::Peekable;
|
|||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ast::{Ast, BinOpType, BlockScope, Expression, If, Loop, Statement, UnOpType},
|
ast::{Ast, BinOpType, BlockScope, Expression, If, Loop, Statement},
|
||||||
stringstore::{Sid, StringStore},
|
stringstore::{Sid, StringStore},
|
||||||
token::Token,
|
token::Token,
|
||||||
T,
|
T,
|
||||||
@ -282,25 +282,11 @@ impl<T: Iterator<Item = Token>> Parser<T> {
|
|||||||
inner_expr
|
inner_expr
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unary negation
|
// Unary operations or invalid token
|
||||||
T![-] => {
|
tok => match tok.try_to_unop() {
|
||||||
let operand = self.parse_primary()?;
|
Some(uot) => Expression::UnOp(uot, self.parse_primary()?.into()),
|
||||||
Expression::UnOp(UnOpType::Negate, operand.into())
|
None => return Err(ParseErr::UnexpectedToken(tok, "primary".to_string())),
|
||||||
}
|
},
|
||||||
|
|
||||||
// Unary bitwise not (bitflip)
|
|
||||||
T![~] => {
|
|
||||||
let operand = self.parse_primary()?;
|
|
||||||
Expression::UnOp(UnOpType::BNot, operand.into())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unary logical not
|
|
||||||
T![!] => {
|
|
||||||
let operand = self.parse_primary()?;
|
|
||||||
Expression::UnOp(UnOpType::LNot, operand.into())
|
|
||||||
}
|
|
||||||
|
|
||||||
tok => return Err(ParseErr::UnexpectedToken(tok, "primary".to_string())),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(primary)
|
Ok(primary)
|
||||||
|
|||||||
12
src/token.rs
12
src/token.rs
@ -1,4 +1,4 @@
|
|||||||
use crate::{ast::BinOpType, T};
|
use crate::{ast::{BinOpType, UnOpType}, T};
|
||||||
|
|
||||||
/// Language keywords
|
/// Language keywords
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
@ -168,6 +168,16 @@ impl Token {
|
|||||||
_ => return None,
|
_ => return None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn try_to_unop(&self) -> Option<UnOpType> {
|
||||||
|
Some(match self {
|
||||||
|
T![-] => UnOpType::Negate,
|
||||||
|
T![!] => UnOpType::LNot,
|
||||||
|
T![~] => UnOpType::BNot,
|
||||||
|
|
||||||
|
_ => return None,
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Macro to quickly create a token of the specified kind
|
/// Macro to quickly create a token of the specified kind
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user