Compare commits
1 Commits
main
...
runtime-pr
Author | SHA1 | Date |
---|---|---|
|
c81c728d54 |
|
@ -3,9 +3,4 @@ name = "leaf"
|
|||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[profile.release]
|
||||
strip = true
|
||||
lto = true
|
||||
codegen-units = 1
|
||||
|
||||
[dependencies]
|
||||
|
|
36
src/kind.rs
36
src/kind.rs
|
@ -1,36 +0,0 @@
|
|||
pub trait Kind {
|
||||
type Kinds;
|
||||
|
||||
fn kind(&self) -> Self::Kinds;
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! kinds {
|
||||
($base:ident, $kind:ident, $( $v:ident $( ( $($vty:ty = $vval:expr),* ) )?),* $(,)?) => {
|
||||
#[derive(Debug)]
|
||||
pub enum $base {
|
||||
$( $v $( ( $($vty),* ) )?, )*
|
||||
}
|
||||
impl $crate::kind::Kind for $base {
|
||||
type Kinds = $kind;
|
||||
|
||||
fn kind(&self) -> $kind {
|
||||
$kind(std::mem::discriminant(self))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Clone, Copy)]
|
||||
pub struct $kind(std::mem::Discriminant<$base>);
|
||||
|
||||
impl $kind {
|
||||
$(
|
||||
#[allow(non_upper_case_globals, dead_code)]
|
||||
pub const $v: Self = $kind (
|
||||
std::mem::discriminant(
|
||||
&( $base::$v $( ( $($vval),* ) )? )
|
||||
)
|
||||
);
|
||||
)*
|
||||
}
|
||||
};
|
||||
}
|
173
src/lexer.rs
173
src/lexer.rs
|
@ -1,24 +1,22 @@
|
|||
use std::{
|
||||
fmt,
|
||||
iter::Peekable,
|
||||
num::{ParseFloatError, ParseIntError},
|
||||
};
|
||||
use std::{fmt, iter::Peekable, rc::Rc};
|
||||
|
||||
use crate::kinds;
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct Ident(String);
|
||||
impl fmt::Display for Ident {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
self.0.fmt(f)
|
||||
}
|
||||
}
|
||||
impl AsRef<str> for Ident {
|
||||
fn as_ref(&self) -> &str {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Literal {
|
||||
String(String),
|
||||
String(Rc<String>),
|
||||
Integer(i64),
|
||||
Float(f64),
|
||||
Boolean(bool),
|
||||
Nil,
|
||||
Ident(Ident),
|
||||
|
@ -28,7 +26,6 @@ impl fmt::Display for Literal {
|
|||
match self {
|
||||
Literal::String(s) => write!(f, "\"{s}\""),
|
||||
Literal::Integer(n) => write!(f, "{n}"),
|
||||
Literal::Float(n) => write!(f, "{n}"),
|
||||
Literal::Boolean(b) => write!(f, "{b}"),
|
||||
Literal::Ident(id) => write!(f, "{id}"),
|
||||
Literal::Nil => write!(f, "nil"),
|
||||
|
@ -36,53 +33,54 @@ impl fmt::Display for Literal {
|
|||
}
|
||||
}
|
||||
|
||||
kinds!(
|
||||
Token,
|
||||
TokenKind,
|
||||
#[derive(Debug)]
|
||||
pub enum Token {
|
||||
Equals,
|
||||
|
||||
Plus,
|
||||
Minus,
|
||||
Star,
|
||||
Slash,
|
||||
Percent,
|
||||
StarStar,
|
||||
PlusEquals,
|
||||
MinusEquals,
|
||||
StarEquals,
|
||||
SlashEquals,
|
||||
Caret,
|
||||
|
||||
CurlyOpen,
|
||||
CurlyClose,
|
||||
|
||||
ParenOpen,
|
||||
ParenClose,
|
||||
|
||||
Comma,
|
||||
Eol,
|
||||
Func,
|
||||
If,
|
||||
Else,
|
||||
|
||||
Return,
|
||||
|
||||
Not,
|
||||
|
||||
EqualTo,
|
||||
NotEqualTo,
|
||||
|
||||
And,
|
||||
Or,
|
||||
|
||||
LessThan,
|
||||
LessThanOrEqualTo,
|
||||
GreaterThan,
|
||||
GreaterThanOrEqualTo,
|
||||
Literal(Literal = Literal::Nil),
|
||||
);
|
||||
|
||||
Literal(Literal),
|
||||
}
|
||||
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
|
||||
pub enum Precedence {
|
||||
Min,
|
||||
Return,
|
||||
Assign,
|
||||
WithAssign,
|
||||
Logical,
|
||||
Equality,
|
||||
Relational,
|
||||
AddSub,
|
||||
MulDivMod,
|
||||
MulDiv,
|
||||
Pow,
|
||||
Prefix,
|
||||
NegateNot,
|
||||
}
|
||||
#[derive(PartialEq, Eq)]
|
||||
pub enum Associativity {
|
||||
|
@ -92,9 +90,8 @@ pub enum Associativity {
|
|||
impl Token {
|
||||
pub fn prefix_precedence(&self) -> Option<Precedence> {
|
||||
Some(match self {
|
||||
Token::Return | Token::If | Token::Func | Token::Minus | Token::Not => {
|
||||
Precedence::Prefix
|
||||
}
|
||||
Token::Return => Precedence::Return,
|
||||
Token::Minus | Token::Not => Precedence::NegateNot,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
@ -107,14 +104,9 @@ impl Token {
|
|||
| Token::GreaterThanOrEqualTo => (Precedence::Relational, Associativity::Left),
|
||||
Token::And | Token::Or => (Precedence::Logical, Associativity::Left),
|
||||
Token::Plus | Token::Minus => (Precedence::AddSub, Associativity::Left),
|
||||
Token::Star | Token::Slash | Token::Percent => {
|
||||
(Precedence::MulDivMod, Associativity::Left)
|
||||
}
|
||||
Token::StarStar => (Precedence::Pow, Associativity::Right),
|
||||
Token::Star | Token::Slash => (Precedence::MulDiv, Associativity::Left),
|
||||
Token::Caret => (Precedence::Pow, Associativity::Right),
|
||||
Token::Equals => (Precedence::Assign, Associativity::Right),
|
||||
Token::PlusEquals | Token::MinusEquals | Token::StarEquals | Token::SlashEquals => {
|
||||
(Precedence::WithAssign, Associativity::Right)
|
||||
}
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
@ -122,8 +114,6 @@ impl Token {
|
|||
|
||||
#[derive(Debug)]
|
||||
pub enum LexError {
|
||||
InvalidInteger(ParseIntError),
|
||||
InvalidFloat(ParseFloatError),
|
||||
InvalidEscape(char),
|
||||
UnexpectedCharacter(char),
|
||||
UnexpectedEnd,
|
||||
|
@ -131,24 +121,12 @@ pub enum LexError {
|
|||
impl fmt::Display for LexError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::InvalidInteger(err) => write!(f, "invalid integer: {err}"),
|
||||
Self::InvalidFloat(err) => write!(f, "invalid float: {err}"),
|
||||
Self::UnexpectedEnd => write!(f, "unexpected end of source"),
|
||||
Self::UnexpectedCharacter(c) => write!(f, "unexpected char '{c}'"),
|
||||
Self::InvalidEscape(c) => write!(f, "\"\\{c}\" is not a valid string escape"),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl From<ParseIntError> for LexError {
|
||||
fn from(err: ParseIntError) -> Self {
|
||||
Self::InvalidInteger(err)
|
||||
}
|
||||
}
|
||||
impl From<ParseFloatError> for LexError {
|
||||
fn from(err: ParseFloatError) -> Self {
|
||||
Self::InvalidFloat(err)
|
||||
}
|
||||
}
|
||||
|
||||
pub type Result<T> = std::result::Result<T, LexError>;
|
||||
|
||||
|
@ -159,10 +137,6 @@ where
|
|||
chars: Peekable<I>,
|
||||
}
|
||||
|
||||
fn t(tk: Token) -> Option<Result<Token>> {
|
||||
Some(Ok(tk))
|
||||
}
|
||||
|
||||
impl<I> Lexer<I>
|
||||
where
|
||||
I: Iterator<Item = char>,
|
||||
|
@ -208,7 +182,7 @@ where
|
|||
fn lex_whitespace(&mut self) -> Option<char> {
|
||||
loop {
|
||||
match self.peek()? {
|
||||
' ' | '\t' | '\n' | '\r' => self.eat(),
|
||||
' ' | '\t' | '\r' => self.eat(),
|
||||
_ => break self.peek(),
|
||||
}
|
||||
}
|
||||
|
@ -222,9 +196,6 @@ where
|
|||
}
|
||||
|
||||
match word.as_str() {
|
||||
"func" => Token::Func,
|
||||
"if" => Token::If,
|
||||
"else" => Token::Else,
|
||||
"return" => Token::Return,
|
||||
"true" => Token::Literal(Literal::Boolean(true)),
|
||||
"false" => Token::Literal(Literal::Boolean(false)),
|
||||
|
@ -233,27 +204,21 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
fn lex_number(&mut self) -> Result<Token> {
|
||||
fn lex_integer(&mut self) -> Token {
|
||||
let mut n_str = String::new();
|
||||
|
||||
// we don't lex negatives. the impl for that is
|
||||
// a negation of a positive number at runtime.
|
||||
// maybe that's kind of stupid though, lol
|
||||
let mut is_float = false;
|
||||
while let Some('0'..='9' | '.') = self.peek() {
|
||||
if self.peek() == Some('.') {
|
||||
is_float = true;
|
||||
}
|
||||
while let Some('0'..='9') = self.peek() {
|
||||
n_str.push(self.next_unwrap());
|
||||
}
|
||||
|
||||
let lit = if is_float {
|
||||
Literal::Float(n_str.parse()?)
|
||||
} else {
|
||||
Literal::Integer(n_str.parse()?)
|
||||
};
|
||||
// we can only read digits 0 to 9 so this should not fail
|
||||
// .. unless we overflow
|
||||
let n = n_str.parse().unwrap();
|
||||
|
||||
Ok(Token::Literal(lit))
|
||||
Token::Literal(Literal::Integer(n))
|
||||
}
|
||||
|
||||
fn lex_string(&mut self) -> Result<Token> {
|
||||
|
@ -274,7 +239,7 @@ where
|
|||
},
|
||||
c if c == delim => {
|
||||
self.eat();
|
||||
break Ok(Token::Literal(Literal::String(str)));
|
||||
break Ok(Token::Literal(Literal::String(Rc::new(str))));
|
||||
}
|
||||
_ => str.push(self.next_unwrap()),
|
||||
}
|
||||
|
@ -293,46 +258,19 @@ where
|
|||
')' => self.eat_to(Token::ParenClose),
|
||||
|
||||
// + add
|
||||
// or += add eq
|
||||
'+' => match self.eat_peek() {
|
||||
Some('=') => self.eat_to(Token::PlusEquals),
|
||||
_ => t(Token::Plus),
|
||||
},
|
||||
'+' => self.eat_to(Token::Plus),
|
||||
|
||||
// - subtract
|
||||
// or -= sub eq
|
||||
'-' => match self.eat_peek() {
|
||||
Some('=') => self.eat_to(Token::MinusEquals),
|
||||
_ => t(Token::Minus),
|
||||
},
|
||||
'-' => self.eat_to(Token::Minus),
|
||||
|
||||
// * multiply
|
||||
// or *= mult eq
|
||||
// or ** pow
|
||||
'*' => match self.eat_peek() {
|
||||
Some('=') => self.eat_to(Token::StarEquals),
|
||||
Some('*') => self.eat_to(Token::StarStar),
|
||||
_ => t(Token::Star),
|
||||
},
|
||||
'*' => self.eat_to(Token::Star),
|
||||
|
||||
// / divide
|
||||
// or /= div eq
|
||||
// or // comment
|
||||
'/' => match self.eat_peek() {
|
||||
Some('=') => self.eat_to(Token::SlashEquals),
|
||||
Some('/') => {
|
||||
// skip the rest of the line
|
||||
// this leaves the newline btw
|
||||
while !matches!(self.peek(), Some('\n') | None) {
|
||||
self.eat();
|
||||
}
|
||||
continue;
|
||||
}
|
||||
_ => t(Token::Slash),
|
||||
},
|
||||
'/' => self.eat_to(Token::Slash),
|
||||
|
||||
// % modulo
|
||||
'%' => self.eat_to(Token::Percent),
|
||||
// ^ pow
|
||||
'^' => self.eat_to(Token::Caret),
|
||||
|
||||
// , comma
|
||||
',' => self.eat_to(Token::Comma),
|
||||
|
@ -341,14 +279,14 @@ where
|
|||
// or == equal to
|
||||
'=' => match self.eat_peek() {
|
||||
Some('=') => self.eat_to(Token::EqualTo),
|
||||
_ => t(Token::Equals),
|
||||
_ => Some(Ok(Token::Equals)),
|
||||
},
|
||||
|
||||
// ! not
|
||||
// or != not equal to
|
||||
'!' => match self.eat_peek() {
|
||||
Some('=') => self.eat_to(Token::NotEqualTo),
|
||||
_ => t(Token::Not),
|
||||
_ => Some(Ok(Token::Not)),
|
||||
},
|
||||
|
||||
// && and
|
||||
|
@ -361,25 +299,38 @@ where
|
|||
// or >= greater than/equal to
|
||||
'>' => match self.eat_peek() {
|
||||
Some('=') => self.eat_to(Token::GreaterThanOrEqualTo),
|
||||
_ => t(Token::GreaterThan),
|
||||
_ => Some(Ok(Token::GreaterThan)),
|
||||
},
|
||||
|
||||
// < less than
|
||||
// or <= less than/equal to
|
||||
'<' => match self.eat_peek() {
|
||||
Some('=') => self.eat_to(Token::LessThanOrEqualTo),
|
||||
_ => t(Token::LessThan),
|
||||
_ => Some(Ok(Token::LessThan)),
|
||||
},
|
||||
|
||||
// a-zA-Z_ start of word
|
||||
'a'..='z' | 'A'..='Z' | '_' => Some(Ok(self.lex_word())),
|
||||
|
||||
// 0-9 integer
|
||||
'0'..='9' | '.' => Some(self.lex_number()),
|
||||
'0'..='9' => Some(Ok(self.lex_integer())),
|
||||
|
||||
// " strings
|
||||
'"' => Some(self.lex_string()),
|
||||
|
||||
// # comments
|
||||
'#' => {
|
||||
// skip the rest of the line
|
||||
// this leaves the newline btw
|
||||
while !matches!(self.peek(), Some('\n') | None) {
|
||||
self.eat();
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// ;, \n eol
|
||||
'\n' | ';' => self.eat_to(Token::Eol),
|
||||
|
||||
// unexpected character
|
||||
c => Some(Err(LexError::UnexpectedCharacter(c))),
|
||||
};
|
||||
|
|
14
src/main.rs
14
src/main.rs
|
@ -1,17 +1,21 @@
|
|||
use std::time::Instant;
|
||||
use std::{rc::Rc, time::Instant};
|
||||
|
||||
use crate::{lexer::Lexer, parser::Parser};
|
||||
use crate::{
|
||||
lexer::Lexer,
|
||||
parser::{Expr, Parser},
|
||||
};
|
||||
|
||||
mod kind;
|
||||
mod lexer;
|
||||
mod parser;
|
||||
mod runtime;
|
||||
|
||||
fn main() {
|
||||
let script = std::fs::read_to_string("./start.lf").unwrap();
|
||||
let script = std::fs::read_to_string("./start.leaf").unwrap();
|
||||
let lexer = Lexer::new(script.chars());
|
||||
let mut parser = Parser::new(lexer.map(Result::unwrap));
|
||||
let start = Instant::now();
|
||||
let block = parser.parse().unwrap();
|
||||
println!("Parse took {:?}", start.elapsed());
|
||||
parser::util::display(parser::Expr::Block(block));
|
||||
// parser::util::display(&Rc::new(Expr::Block(Rc::new(block))));
|
||||
runtime::exec(&block).unwrap();
|
||||
}
|
||||
|
|
245
src/parser.rs
245
src/parser.rs
|
@ -1,54 +1,43 @@
|
|||
use std::{fmt, iter::Peekable};
|
||||
use std::{fmt, iter::Peekable, rc::Rc};
|
||||
|
||||
use crate::{
|
||||
kind::Kind,
|
||||
lexer::{Associativity, LexError, Literal, Precedence, Token, TokenKind},
|
||||
};
|
||||
use crate::lexer::{Associativity, LexError, Literal, Precedence, Token};
|
||||
|
||||
pub mod util;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Expr {
|
||||
// Data and variables
|
||||
Assign(Box<Expr>, Box<Expr>),
|
||||
Assignment(Rc<Expr>, Rc<Expr>),
|
||||
Literal(Literal),
|
||||
// Non-literal datatypes
|
||||
Block(Block),
|
||||
Func(Vec<Expr>, Box<Expr>),
|
||||
// Runtime datatypes
|
||||
Block(Rc<Block>),
|
||||
// Control flow
|
||||
If(Box<Expr>, Box<Expr>, Option<Box<Expr>>),
|
||||
Return(Box<Expr>),
|
||||
Call(Box<Expr>, Vec<Expr>),
|
||||
Return(Rc<Expr>),
|
||||
Call(Rc<Expr>, Vec<Rc<Expr>>),
|
||||
// Unary operations
|
||||
Negate(Box<Expr>),
|
||||
Not(Box<Expr>),
|
||||
Negate(Rc<Expr>),
|
||||
Not(Rc<Expr>),
|
||||
// Binary operations: logical
|
||||
EqualTo(Box<Expr>, Box<Expr>),
|
||||
NotEqualTo(Box<Expr>, Box<Expr>),
|
||||
And(Box<Expr>, Box<Expr>),
|
||||
Or(Box<Expr>, Box<Expr>),
|
||||
EqualTo(Rc<Expr>, Rc<Expr>),
|
||||
NotEqualTo(Rc<Expr>, Rc<Expr>),
|
||||
And(Rc<Expr>, Rc<Expr>),
|
||||
Or(Rc<Expr>, Rc<Expr>),
|
||||
// Binary operations: comparison
|
||||
LessThan(Box<Expr>, Box<Expr>),
|
||||
LessThanOrEqualTo(Box<Expr>, Box<Expr>),
|
||||
GreaterThan(Box<Expr>, Box<Expr>),
|
||||
GreaterThanOrEqualTo(Box<Expr>, Box<Expr>),
|
||||
LessThan(Rc<Expr>, Rc<Expr>),
|
||||
LessThanOrEqualTo(Rc<Expr>, Rc<Expr>),
|
||||
GreaterThan(Rc<Expr>, Rc<Expr>),
|
||||
GreaterThanOrEqualTo(Rc<Expr>, Rc<Expr>),
|
||||
// Binary operations: arithmetic
|
||||
Add(Box<Expr>, Box<Expr>),
|
||||
Subtract(Box<Expr>, Box<Expr>),
|
||||
Multiply(Box<Expr>, Box<Expr>),
|
||||
Divide(Box<Expr>, Box<Expr>),
|
||||
Exponent(Box<Expr>, Box<Expr>),
|
||||
Modulo(Box<Expr>, Box<Expr>),
|
||||
// Binary operations: arithmetic w/ assignment
|
||||
AddAssign(Box<Expr>, Box<Expr>),
|
||||
SubtractAssign(Box<Expr>, Box<Expr>),
|
||||
MultiplyAssign(Box<Expr>, Box<Expr>),
|
||||
DivideAssign(Box<Expr>, Box<Expr>),
|
||||
Add(Rc<Expr>, Rc<Expr>),
|
||||
Subtract(Rc<Expr>, Rc<Expr>),
|
||||
Multiply(Rc<Expr>, Rc<Expr>),
|
||||
Divide(Rc<Expr>, Rc<Expr>),
|
||||
Exponent(Rc<Expr>, Rc<Expr>),
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Block {
|
||||
pub exprs: Vec<Expr>,
|
||||
pub exprs: Vec<Rc<Expr>>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
@ -76,6 +65,7 @@ pub type Result<T> = std::result::Result<T, ParseError>;
|
|||
|
||||
pub struct Parser<I: Iterator<Item = Token>> {
|
||||
tokens: Peekable<I>,
|
||||
is_next_eol: bool,
|
||||
}
|
||||
impl<I> Parser<I>
|
||||
where
|
||||
|
@ -83,7 +73,10 @@ where
|
|||
{
|
||||
pub fn new(tokens: I) -> Self {
|
||||
let tokens = tokens.peekable();
|
||||
Self { tokens }
|
||||
Self {
|
||||
tokens,
|
||||
is_next_eol: false,
|
||||
}
|
||||
}
|
||||
|
||||
fn eat(&mut self) {
|
||||
|
@ -92,37 +85,33 @@ where
|
|||
fn next_unwrap(&mut self) -> Token {
|
||||
self.try_next().unwrap()
|
||||
}
|
||||
fn skip_eol(&mut self) -> bool {
|
||||
let mut did_skip = false;
|
||||
|
||||
while matches!(self.tokens.peek(), Some(Token::Eol)) {
|
||||
self.tokens.next();
|
||||
did_skip = true;
|
||||
}
|
||||
|
||||
did_skip
|
||||
}
|
||||
fn try_peek(&mut self) -> Result<&Token> {
|
||||
// Peek doesn't advance the token stream, so
|
||||
// don't allow it to unset the EOL flag
|
||||
if self.skip_eol() {
|
||||
self.is_next_eol = true;
|
||||
}
|
||||
self.tokens.peek().ok_or(ParseError::UnexpectedEnd)
|
||||
}
|
||||
fn try_next(&mut self) -> Result<Token> {
|
||||
self.is_next_eol = self.skip_eol();
|
||||
self.tokens.next().ok_or(ParseError::UnexpectedEnd)
|
||||
}
|
||||
|
||||
fn expect_next(&mut self, kind: TokenKind) -> Result<()> {
|
||||
let t = self.try_next()?;
|
||||
|
||||
if t.kind() != kind {
|
||||
return Err(ParseError::UnexpectedToken(t));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
fn is_next(&mut self, kind: Option<TokenKind>) -> bool {
|
||||
match self.try_peek() {
|
||||
Ok(t) if Some(t.kind()) == kind => true,
|
||||
Ok(_) => false,
|
||||
|
||||
Err(ParseError::UnexpectedEnd) if kind.is_none() => true,
|
||||
Err(_) => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_expr(&mut self, min_prec: Precedence, in_group: bool) -> Result<Box<Expr>> {
|
||||
fn parse_expr(&mut self, min_prec: Precedence, in_group: bool) -> Result<Rc<Expr>> {
|
||||
let mut lhs = match self.try_next()? {
|
||||
// literal
|
||||
Token::Literal(lit) => Box::new(Expr::Literal(lit)),
|
||||
Token::Literal(lit) => Rc::new(Expr::Literal(lit)),
|
||||
|
||||
// start of group
|
||||
Token::ParenOpen => {
|
||||
|
@ -134,57 +123,22 @@ where
|
|||
}
|
||||
// start of a block
|
||||
Token::CurlyOpen => {
|
||||
let exprs = self.parse_until(Some(TokenKind::CurlyClose))?;
|
||||
let b = self.parse_block(true)?;
|
||||
// skip curly brace
|
||||
self.eat();
|
||||
Box::new(Expr::Block(Block { exprs }))
|
||||
Rc::new(Expr::Block(Rc::new(b)))
|
||||
}
|
||||
|
||||
// unary ops!! (prefix)
|
||||
t if t.prefix_precedence().is_some() => {
|
||||
let prec = t.prefix_precedence().unwrap();
|
||||
match t {
|
||||
// parse function
|
||||
Token::Func => {
|
||||
// parse args
|
||||
self.expect_next(TokenKind::ParenOpen)?;
|
||||
let args = self
|
||||
.parse_delimited_until(TokenKind::Comma, Some(TokenKind::ParenClose))?;
|
||||
self.eat();
|
||||
// parse body
|
||||
let body = self.parse_expr(prec, in_group)?;
|
||||
// pack
|
||||
Box::new(Expr::Func(args, body))
|
||||
}
|
||||
// parse if
|
||||
Token::If => {
|
||||
// parse the condition
|
||||
let cond = self.parse_expr(Precedence::Min, false)?;
|
||||
// parse the true case
|
||||
let true_case = self.parse_expr(prec, in_group)?;
|
||||
// and maybe a false case
|
||||
let false_case = matches!(self.try_peek(), Ok(Token::Else))
|
||||
.then(|| {
|
||||
self.eat();
|
||||
self.parse_expr(prec, in_group)
|
||||
})
|
||||
.transpose()?;
|
||||
// pack
|
||||
Box::new(Expr::If(cond, true_case, false_case))
|
||||
}
|
||||
|
||||
// another op
|
||||
_ => {
|
||||
let rhs = self.parse_expr(prec, in_group)?;
|
||||
Box::new(match t {
|
||||
Token::Minus => Expr::Negate(rhs),
|
||||
Token::Not => Expr::Not(rhs),
|
||||
Token::Return => Expr::Return(rhs),
|
||||
|
||||
_ => unreachable!(),
|
||||
})
|
||||
}
|
||||
}
|
||||
let rhs = self.parse_expr(prec, in_group)?;
|
||||
Rc::new(match t {
|
||||
Token::Minus => Expr::Negate(rhs),
|
||||
Token::Not => Expr::Not(rhs),
|
||||
Token::Return => Expr::Return(rhs),
|
||||
_ => unreachable!(),
|
||||
})
|
||||
}
|
||||
|
||||
// unexpected token
|
||||
|
@ -205,16 +159,29 @@ where
|
|||
|
||||
// function call
|
||||
Ok(Token::ParenOpen) => {
|
||||
if self.is_next_eol {
|
||||
break;
|
||||
}
|
||||
|
||||
// eat opening paren
|
||||
self.eat();
|
||||
|
||||
let exprs =
|
||||
self.parse_delimited_until(TokenKind::Comma, Some(TokenKind::ParenClose))?;
|
||||
let mut exprs = Vec::new();
|
||||
while !matches!(self.try_peek()?, Token::ParenClose) {
|
||||
exprs.push(self.parse_expr(Precedence::Min, false)?);
|
||||
|
||||
// Continue if there is a comma,
|
||||
// ignore closing parens
|
||||
match self.try_peek()? {
|
||||
Token::Comma => self.eat(),
|
||||
Token::ParenClose => {}
|
||||
_ => return Err(ParseError::UnexpectedToken(self.next_unwrap())),
|
||||
}
|
||||
}
|
||||
// eat closing paren
|
||||
self.eat();
|
||||
|
||||
lhs = Box::new(Expr::Call(lhs, exprs));
|
||||
lhs = Rc::new(Expr::Call(lhs, exprs));
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -225,7 +192,7 @@ where
|
|||
let (prec, assoc) = op.infix_precedence().unwrap();
|
||||
|
||||
// break if this op is meant for previous recursion
|
||||
// or it's equal and we prefer to build leftward
|
||||
// or it's equal and we would prefer to build leftward..
|
||||
if prec < min_prec || (prec == min_prec && assoc == Associativity::Left) {
|
||||
break;
|
||||
}
|
||||
|
@ -236,7 +203,7 @@ where
|
|||
let rhs = self.parse_expr(prec, in_group)?;
|
||||
|
||||
// join to lhs
|
||||
lhs = Box::new(match op {
|
||||
lhs = Rc::new(match op {
|
||||
// equality
|
||||
Token::EqualTo => Expr::EqualTo(lhs, rhs),
|
||||
Token::NotEqualTo => Expr::NotEqualTo(lhs, rhs),
|
||||
|
@ -251,18 +218,13 @@ where
|
|||
// add, subtract
|
||||
Token::Plus => Expr::Add(lhs, rhs),
|
||||
Token::Minus => Expr::Subtract(lhs, rhs),
|
||||
Token::PlusEquals => Expr::AddAssign(lhs, rhs),
|
||||
Token::MinusEquals => Expr::SubtractAssign(lhs, rhs),
|
||||
// multiply, divide
|
||||
Token::Star => Expr::Multiply(lhs, rhs),
|
||||
Token::Slash => Expr::Divide(lhs, rhs),
|
||||
Token::StarEquals => Expr::MultiplyAssign(lhs, rhs),
|
||||
Token::SlashEquals => Expr::DivideAssign(lhs, rhs),
|
||||
// exponent, modulo
|
||||
Token::StarStar => Expr::Exponent(lhs, rhs),
|
||||
Token::Percent => Expr::Modulo(lhs, rhs),
|
||||
// exponent
|
||||
Token::Caret => Expr::Exponent(lhs, rhs),
|
||||
// assignment
|
||||
Token::Equals => Expr::Assign(lhs, rhs),
|
||||
Token::Equals => Expr::Assignment(lhs, rhs),
|
||||
// unreachable as all tokens with precedences are covered above
|
||||
_ => unreachable!(),
|
||||
});
|
||||
|
@ -270,53 +232,26 @@ where
|
|||
|
||||
Ok(lhs)
|
||||
}
|
||||
|
||||
fn parse_until(&mut self, until: Option<TokenKind>) -> Result<Vec<Expr>> {
|
||||
fn parse_block(&mut self, in_block: bool) -> Result<Block> {
|
||||
let mut exprs = Vec::new();
|
||||
loop {
|
||||
match self.try_peek() {
|
||||
// end (block)
|
||||
Ok(Token::CurlyClose) if in_block => break,
|
||||
// end (stream) lpwkey idk if this is a good way to check for error
|
||||
// need to add error nodes anyway so whatever
|
||||
Err(ParseError::UnexpectedEnd) if !in_block => break,
|
||||
|
||||
while !self.is_next(until) {
|
||||
// try to parse expr
|
||||
exprs.push(*self.parse_expr(Precedence::Min, false)?);
|
||||
// try to parse expr
|
||||
Ok(_) => exprs.push(self.parse_expr(Precedence::Min, false)?),
|
||||
|
||||
// check for end
|
||||
if self.is_next(until) {
|
||||
break;
|
||||
// invalid
|
||||
Err(err) => return Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
Ok(exprs)
|
||||
}
|
||||
|
||||
fn parse_delimited_until(
|
||||
&mut self,
|
||||
delim: TokenKind,
|
||||
until: Option<TokenKind>,
|
||||
) -> Result<Vec<Expr>> {
|
||||
let mut exprs = Vec::new();
|
||||
|
||||
while !self.is_next(until) {
|
||||
// skip delimiter
|
||||
if self.is_next(Some(delim)) {
|
||||
self.eat();
|
||||
continue;
|
||||
}
|
||||
|
||||
// try to parse expr
|
||||
exprs.push(*self.parse_expr(Precedence::Min, false)?);
|
||||
|
||||
// check for end
|
||||
if self.is_next(until) {
|
||||
break;
|
||||
}
|
||||
|
||||
// check for delim
|
||||
self.expect_next(delim)?;
|
||||
}
|
||||
Ok(exprs)
|
||||
}
|
||||
|
||||
pub fn parse(&mut self) -> Result<Block> {
|
||||
let exprs = self.parse_until(None)?;
|
||||
Ok(Block { exprs })
|
||||
}
|
||||
pub fn parse(&mut self) -> Result<Block> {
|
||||
self.parse_block(false)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
use std::fmt::Write;
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::parser::Expr;
|
||||
|
||||
pub fn display(e: Expr) {
|
||||
pub fn display(e: &Rc<Expr>) {
|
||||
let result = fmt_expr(e, 0);
|
||||
println!("{result}");
|
||||
}
|
||||
|
||||
fn fmt_binop(left: Expr, right: Expr, op: &str, depth: usize) -> String {
|
||||
fn fmt_binop(left: &Rc<Expr>, right: &Rc<Expr>, op: &str, depth: usize) -> String {
|
||||
format!(
|
||||
"({} {} {})",
|
||||
fmt_expr(left, depth),
|
||||
|
@ -16,19 +16,15 @@ fn fmt_binop(left: Expr, right: Expr, op: &str, depth: usize) -> String {
|
|||
)
|
||||
}
|
||||
|
||||
fn fmt_expr(e: Expr, depth: usize) -> String {
|
||||
match e {
|
||||
Expr::Assign(l, r) => fmt_binop(*l, *r, "=", depth),
|
||||
Expr::AddAssign(l, r) => fmt_binop(*l, *r, "+=", depth),
|
||||
Expr::SubtractAssign(l, r) => fmt_binop(*l, *r, "-=", depth),
|
||||
Expr::MultiplyAssign(l, r) => fmt_binop(*l, *r, "*=", depth),
|
||||
Expr::DivideAssign(l, r) => fmt_binop(*l, *r, "/=", depth),
|
||||
fn fmt_expr(e: &Rc<Expr>, depth: usize) -> String {
|
||||
match &**e {
|
||||
Expr::Assignment(l, r) => fmt_binop(l, r, "=", depth),
|
||||
Expr::Literal(l) => l.to_string(),
|
||||
Expr::Call(l, r) => {
|
||||
let mut result = fmt_expr(*l, depth);
|
||||
let mut result = fmt_expr(l, depth);
|
||||
result.push('(');
|
||||
let len = r.len();
|
||||
for (i, e) in r.into_iter().enumerate() {
|
||||
for (i, e) in r.iter().enumerate() {
|
||||
result.push_str(&fmt_expr(e, depth));
|
||||
if i + 1 != len {
|
||||
result.push_str(", ");
|
||||
|
@ -37,18 +33,11 @@ fn fmt_expr(e: Expr, depth: usize) -> String {
|
|||
result.push(')');
|
||||
result
|
||||
}
|
||||
Expr::If(c, t, f) => {
|
||||
let mut result = format!("if ({}) ({})", fmt_expr(*c, depth), fmt_expr(*t, depth));
|
||||
if let Some(f) = f {
|
||||
let _ = write!(result, " else ({})", fmt_expr(*f, depth));
|
||||
}
|
||||
result
|
||||
}
|
||||
Expr::Return(l) => format!("return ({})", fmt_expr(*l, depth)),
|
||||
Expr::Return(r) => format!("return {}", fmt_expr(r, depth)),
|
||||
Expr::Block(b) => {
|
||||
let mut result = String::new();
|
||||
let len = b.exprs.len();
|
||||
for (i, expr) in b.exprs.into_iter().enumerate() {
|
||||
for (i, expr) in b.exprs.iter().enumerate() {
|
||||
result.push_str(&" ".repeat(depth));
|
||||
result.push_str(&fmt_expr(expr, depth + 1));
|
||||
if depth != 0 || i + 1 != len {
|
||||
|
@ -60,29 +49,20 @@ fn fmt_expr(e: Expr, depth: usize) -> String {
|
|||
}
|
||||
result
|
||||
}
|
||||
Expr::Func(a, e) => format!(
|
||||
"(func({}) ({}))",
|
||||
a.into_iter()
|
||||
.map(|e| fmt_expr(e, depth))
|
||||
.collect::<Vec<_>>()
|
||||
.join(", "),
|
||||
fmt_expr(*e, depth)
|
||||
),
|
||||
Expr::Negate(l) => format!("(-{})", fmt_expr(*l, depth)),
|
||||
Expr::Not(l) => format!("(!{})", fmt_expr(*l, depth)),
|
||||
Expr::EqualTo(l, r) => fmt_binop(*l, *r, "==", depth),
|
||||
Expr::NotEqualTo(l, r) => fmt_binop(*l, *r, "!=", depth),
|
||||
Expr::And(l, r) => fmt_binop(*l, *r, "&&", depth),
|
||||
Expr::Or(l, r) => fmt_binop(*l, *r, "||", depth),
|
||||
Expr::LessThan(l, r) => fmt_binop(*l, *r, "<", depth),
|
||||
Expr::LessThanOrEqualTo(l, r) => fmt_binop(*l, *r, "<=", depth),
|
||||
Expr::GreaterThan(l, r) => fmt_binop(*l, *r, ">", depth),
|
||||
Expr::GreaterThanOrEqualTo(l, r) => fmt_binop(*l, *r, ">=", depth),
|
||||
Expr::Add(l, r) => fmt_binop(*l, *r, "+", depth),
|
||||
Expr::Subtract(l, r) => fmt_binop(*l, *r, "-", depth),
|
||||
Expr::Multiply(l, r) => fmt_binop(*l, *r, "*", depth),
|
||||
Expr::Divide(l, r) => fmt_binop(*l, *r, "/", depth),
|
||||
Expr::Exponent(l, r) => fmt_binop(*l, *r, "**", depth),
|
||||
Expr::Modulo(l, r) => fmt_binop(*l, *r, "%", depth),
|
||||
Expr::Negate(r) => format!("(-{})", fmt_expr(r, depth)),
|
||||
Expr::Not(r) => format!("(!{})", fmt_expr(r, depth)),
|
||||
Expr::EqualTo(l, r) => fmt_binop(l, r, "==", depth),
|
||||
Expr::NotEqualTo(l, r) => fmt_binop(l, r, "!=", depth),
|
||||
Expr::And(l, r) => fmt_binop(l, r, "&&", depth),
|
||||
Expr::Or(l, r) => fmt_binop(l, r, "||", depth),
|
||||
Expr::LessThan(l, r) => fmt_binop(l, r, "<", depth),
|
||||
Expr::LessThanOrEqualTo(l, r) => fmt_binop(l, r, "<=", depth),
|
||||
Expr::GreaterThan(l, r) => fmt_binop(l, r, ">", depth),
|
||||
Expr::GreaterThanOrEqualTo(l, r) => fmt_binop(l, r, ">=", depth),
|
||||
Expr::Add(l, r) => fmt_binop(l, r, "+", depth),
|
||||
Expr::Subtract(l, r) => fmt_binop(l, r, "-", depth),
|
||||
Expr::Multiply(l, r) => fmt_binop(l, r, "*", depth),
|
||||
Expr::Divide(l, r) => fmt_binop(l, r, "/", depth),
|
||||
Expr::Exponent(l, r) => fmt_binop(l, r, "^", depth),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,303 @@
|
|||
use std::{cell::LazyCell, collections::HashMap, fmt, fmt::Write as _, rc::Rc};
|
||||
|
||||
use crate::{
|
||||
lexer::{Ident, Literal},
|
||||
parser::{Block, Expr},
|
||||
};
|
||||
|
||||
macro_rules! type_error {
|
||||
() => {
|
||||
return Err(RuntimeError::Type.into())
|
||||
};
|
||||
}
|
||||
macro_rules! uncallable {
|
||||
() => {
|
||||
return Err(RuntimeError::Uncallable.into())
|
||||
};
|
||||
}
|
||||
|
||||
use Expr as E;
|
||||
use Literal as L;
|
||||
use Value as V;
|
||||
|
||||
fn builtin_print(args: Args) -> RuntimeResult {
|
||||
let mut result = String::new();
|
||||
for v in args {
|
||||
let _ = write!(result, "{v}\t");
|
||||
}
|
||||
println!("{result}");
|
||||
Ok(V::nil())
|
||||
}
|
||||
fn builtin_if(args: Args) -> RuntimeResult {
|
||||
let Some(Value::Boolean(cond)) = args.get(0).map(|v| &**v) else {
|
||||
type_error!()
|
||||
};
|
||||
let cond = *cond;
|
||||
|
||||
let b_else = args.get(2).map(|v| &**v);
|
||||
let b_if = args.get(1).map(|v| &**v);
|
||||
|
||||
let empty = Vec::new();
|
||||
Ok(match (b_if, b_else) {
|
||||
(Some(V::Block(b_if)), _) if cond => exec_block(&b_if, empty)?,
|
||||
(_, Some(V::Block(b_else))) if !cond => exec_block(&b_else, empty)?,
|
||||
(Some(V::Nil) | None, _) => V::nil(),
|
||||
(_, Some(V::Nil) | None) => V::nil(),
|
||||
(_, _) => uncallable!(),
|
||||
})
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
static NIL: LazyCell<Rc<Value>> = const { LazyCell::new(|| Rc::new(V::Nil)) };
|
||||
static TRUE: LazyCell<Rc<Value>> = const { LazyCell::new(|| Rc::new(V::Boolean(true))) };
|
||||
static FALSE: LazyCell<Rc<Value>> = const { LazyCell::new(|| Rc::new(V::Boolean(false))) };
|
||||
}
|
||||
|
||||
pub enum Value {
|
||||
String(Rc<String>),
|
||||
Integer(i64),
|
||||
Boolean(bool),
|
||||
Block(Rc<Block>),
|
||||
BuiltinFn(fn(Args) -> RuntimeResult),
|
||||
Nil,
|
||||
}
|
||||
impl Value {
|
||||
fn bool(b: bool) -> Rc<Self> {
|
||||
if b {
|
||||
Self::bool_true()
|
||||
} else {
|
||||
Self::bool_false()
|
||||
}
|
||||
}
|
||||
fn int(i: i64) -> Rc<Self> {
|
||||
Rc::new(V::Integer(i))
|
||||
}
|
||||
fn str(s: Rc<String>) -> Rc<Self> {
|
||||
Rc::new(V::String(s))
|
||||
}
|
||||
fn block(b: Rc<Block>) -> Rc<Self> {
|
||||
Rc::new(V::Block(b))
|
||||
}
|
||||
fn builtin_fn(f: fn(Args) -> RuntimeResult) -> Rc<Self> {
|
||||
Rc::new(V::BuiltinFn(f))
|
||||
}
|
||||
fn bool_true() -> Rc<Self> {
|
||||
TRUE.with(|v| (**v).clone())
|
||||
}
|
||||
fn bool_false() -> Rc<Self> {
|
||||
FALSE.with(|v| (**v).clone())
|
||||
}
|
||||
fn nil() -> Rc<Self> {
|
||||
NIL.with(|v| (**v).clone())
|
||||
}
|
||||
}
|
||||
impl PartialEq for Value {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
match (self, other) {
|
||||
(V::String(s1), V::String(s2)) => s1 == s2,
|
||||
(V::Integer(i1), V::Integer(i2)) => i1 == i2,
|
||||
(V::Boolean(b1), V::Boolean(b2)) => b1 == b2,
|
||||
(V::Nil, V::Nil) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl fmt::Display for Value {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
V::String(s) => write!(f, "{s}"),
|
||||
V::Integer(i) => write!(f, "{i}"),
|
||||
V::Boolean(b) => write!(f, "{b}"),
|
||||
V::Block(_b) => write!(f, "<block>"),
|
||||
V::BuiltinFn(_f) => write!(f, "<builtin>"),
|
||||
V::Nil => write!(f, "nil"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum RuntimeError {
|
||||
Type,
|
||||
Uncallable,
|
||||
}
|
||||
impl fmt::Display for RuntimeError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::Type => write!(f, "type error"),
|
||||
Self::Uncallable => write!(f, "uncallable"),
|
||||
}
|
||||
}
|
||||
}
|
||||
pub type RuntimeResult = Result<Rc<Value>, RuntimeError>;
|
||||
|
||||
type Args = Vec<Rc<Value>>;
|
||||
|
||||
enum EvalOutcome {
|
||||
EarlyReturn(Rc<Value>),
|
||||
Error(RuntimeError),
|
||||
}
|
||||
impl From<RuntimeError> for EvalOutcome {
|
||||
fn from(err: RuntimeError) -> Self {
|
||||
Self::Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
fn eval(scope: &mut HashMap<Ident, Rc<Value>>, e: &Rc<Expr>) -> Result<Rc<Value>, EvalOutcome> {
|
||||
Ok(match &**e {
|
||||
E::Assignment(l, r) => {
|
||||
let Expr::Literal(Literal::Ident(id)) = &**l else {
|
||||
type_error!()
|
||||
};
|
||||
let v = eval(scope, r)?;
|
||||
scope.insert(id.clone(), v.clone());
|
||||
v
|
||||
}
|
||||
E::Literal(L::Ident(id)) => match id.as_ref() {
|
||||
"print" => V::builtin_fn(builtin_print),
|
||||
"if" => V::builtin_fn(builtin_if),
|
||||
_ => scope.get(id).cloned().unwrap_or_else(V::nil),
|
||||
},
|
||||
E::Literal(L::String(s)) => V::str(s.clone()),
|
||||
E::Literal(L::Integer(i)) => V::int(*i),
|
||||
E::Literal(L::Boolean(b)) => V::bool(*b),
|
||||
E::Literal(L::Nil) => V::nil(),
|
||||
E::Block(b) => V::block(b.clone()),
|
||||
E::Return(r) => {
|
||||
let r = eval(scope, r)?;
|
||||
return Err(EvalOutcome::EarlyReturn(r));
|
||||
}
|
||||
E::Call(l, r) => {
|
||||
let l = eval(scope, l)?;
|
||||
let args = r.iter().map(|e| eval(scope, e)).collect::<Result<_, _>>()?;
|
||||
match &*l {
|
||||
V::BuiltinFn(f) => f(args)?,
|
||||
V::Block(b) => exec_block(b, args)?,
|
||||
_ => uncallable!(),
|
||||
}
|
||||
}
|
||||
E::Negate(r) => {
|
||||
let l = eval(scope, r)?;
|
||||
let V::Integer(i) = *l else { type_error!() };
|
||||
V::int(-i)
|
||||
}
|
||||
E::Not(r) => {
|
||||
let l = eval(scope, r)?;
|
||||
let V::Boolean(b) = *l else { type_error!() };
|
||||
V::bool(!b)
|
||||
}
|
||||
E::EqualTo(l, r) => {
|
||||
let (l, r) = (eval(scope, l)?, eval(scope, r)?);
|
||||
V::bool(*l == *r)
|
||||
}
|
||||
E::NotEqualTo(l, r) => {
|
||||
let (l, r) = (eval(scope, l)?, eval(scope, r)?);
|
||||
V::bool(*l != *r)
|
||||
}
|
||||
E::And(l, r) => {
|
||||
let (l, r) = (eval(scope, l)?, eval(scope, r)?);
|
||||
V::bool(if let (V::Boolean(b1), V::Boolean(b2)) = (&*l, &*r) {
|
||||
*b1 && *b2
|
||||
} else {
|
||||
type_error!()
|
||||
})
|
||||
}
|
||||
E::Or(l, r) => {
|
||||
let (l, r) = (eval(scope, l)?, eval(scope, r)?);
|
||||
V::bool(if let (V::Boolean(b1), V::Boolean(b2)) = (&*l, &*r) {
|
||||
*b1 || *b2
|
||||
} else {
|
||||
type_error!()
|
||||
})
|
||||
}
|
||||
E::LessThan(l, r) => {
|
||||
let (l, r) = (eval(scope, l)?, eval(scope, r)?);
|
||||
V::bool(if let (V::Integer(i1), V::Integer(i2)) = (&*l, &*r) {
|
||||
i1 < i2
|
||||
} else {
|
||||
type_error!()
|
||||
})
|
||||
}
|
||||
E::LessThanOrEqualTo(l, r) => {
|
||||
let (l, r) = (eval(scope, l)?, eval(scope, r)?);
|
||||
V::bool(if let (V::Integer(i1), V::Integer(i2)) = (&*l, &*r) {
|
||||
i1 <= i2
|
||||
} else {
|
||||
type_error!()
|
||||
})
|
||||
}
|
||||
E::GreaterThan(l, r) => {
|
||||
let (l, r) = (eval(scope, l)?, eval(scope, r)?);
|
||||
V::bool(if let (V::Integer(i1), V::Integer(i2)) = (&*l, &*r) {
|
||||
i1 > i2
|
||||
} else {
|
||||
type_error!()
|
||||
})
|
||||
}
|
||||
E::GreaterThanOrEqualTo(l, r) => {
|
||||
let (l, r) = (eval(scope, l)?, eval(scope, r)?);
|
||||
V::bool(if let (V::Integer(i1), V::Integer(i2)) = (&*l, &*r) {
|
||||
i1 >= i2
|
||||
} else {
|
||||
type_error!()
|
||||
})
|
||||
}
|
||||
E::Add(l, r) => {
|
||||
let (l, r) = (eval(scope, l)?, eval(scope, r)?);
|
||||
V::int(if let (V::Integer(i1), V::Integer(i2)) = (&*l, &*r) {
|
||||
i1 + i2
|
||||
} else {
|
||||
type_error!()
|
||||
})
|
||||
}
|
||||
E::Subtract(l, r) => {
|
||||
let (l, r) = (eval(scope, l)?, eval(scope, r)?);
|
||||
V::int(if let (V::Integer(i1), V::Integer(i2)) = (&*l, &*r) {
|
||||
i1 + i2
|
||||
} else {
|
||||
type_error!()
|
||||
})
|
||||
}
|
||||
E::Multiply(l, r) => {
|
||||
let (l, r) = (eval(scope, l)?, eval(scope, r)?);
|
||||
V::int(if let (V::Integer(i1), V::Integer(i2)) = (&*l, &*r) {
|
||||
i1 * i2
|
||||
} else {
|
||||
type_error!()
|
||||
})
|
||||
}
|
||||
E::Divide(l, r) => {
|
||||
let (l, r) = (eval(scope, l)?, eval(scope, r)?);
|
||||
V::int(if let (V::Integer(i1), V::Integer(i2)) = (&*l, &*r) {
|
||||
i1 / i2
|
||||
} else {
|
||||
type_error!()
|
||||
})
|
||||
}
|
||||
E::Exponent(l, r) => {
|
||||
let (l, r) = (eval(scope, l)?, eval(scope, r)?);
|
||||
V::int(if let (V::Integer(i1), V::Integer(i2)) = (&*l, &*r) {
|
||||
i1.pow(*i2 as u32)
|
||||
} else {
|
||||
type_error!()
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn exec_block(b: &Block, _args: Args) -> RuntimeResult {
|
||||
let mut scope = HashMap::new();
|
||||
|
||||
for e in &b.exprs {
|
||||
match eval(&mut scope, e) {
|
||||
Ok(_) => {}
|
||||
Err(EvalOutcome::EarlyReturn(v)) => return Ok(v),
|
||||
Err(EvalOutcome::Error(err)) => return Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
Ok(V::nil())
|
||||
}
|
||||
|
||||
pub fn exec(b: &Block) -> RuntimeResult {
|
||||
exec_block(b, Vec::new())
|
||||
}
|
Loading…
Reference in New Issue