Implement AST for 'const' expressions. TODO -- add const expressions to compiler

This commit is contained in:
2025-12-04 18:01:16 -07:00
parent e05f130040
commit 4b6c7eb63c
9 changed files with 123 additions and 19 deletions

View File

@@ -462,6 +462,15 @@ impl<'a> Parser<'a> {
})
}
TokenType::Keyword(Keyword::Const) => {
let spanned_const = self.spanned(|p| p.const_declaration())?;
Some(Spanned {
span: spanned_const.span,
node: Expression::ConstDeclaration(spanned_const),
})
}
TokenType::Keyword(Keyword::Fn) => {
let spanned_fn = self.spanned(|p| p.function())?;
Some(Spanned {
@@ -1220,6 +1229,46 @@ impl<'a> Parser<'a> {
Ok(BlockExpression(expressions))
}
fn const_declaration(&mut self) -> Result<ConstDeclarationExpression, Error> {
// const
let current_token = self.current_token.as_ref().ok_or(Error::UnexpectedEOF)?;
if !self_matches_current!(self, TokenType::Keyword(Keyword::Const)) {
return Err(Error::UnexpectedToken(
self.current_span(),
current_token.clone(),
));
}
// variable_name
let ident_token = self.get_next()?.ok_or(Error::UnexpectedEOF)?;
let ident_span = Self::token_to_span(ident_token);
let ident = match ident_token.token_type {
TokenType::Identifier(ref id) => id.clone(),
_ => return Err(Error::UnexpectedToken(ident_span, ident_token.clone())),
};
// `=`
let assign_token = self.get_next()?.ok_or(Error::UnexpectedEOF)?.clone();
if !token_matches!(assign_token, TokenType::Symbol(Symbol::Assign)) {
return Err(Error::UnexpectedToken(
Self::token_to_span(&assign_token),
assign_token,
));
}
// literal value
self.assign_next()?;
let lit = self.spanned(|p| p.literal())?;
Ok(ConstDeclarationExpression {
name: Spanned {
span: ident_span,
node: ident,
},
value: lit,
})
}
fn declaration(&mut self) -> Result<Expression, Error> {
let current_token = self.current_token.as_ref().ok_or(Error::UnexpectedEOF)?;
if !self_matches_current!(self, TokenType::Keyword(Keyword::Let)) {

View File

@@ -1,12 +0,0 @@
use crate::sys_call;
use helpers::Documentation;
use pretty_assertions::assert_eq;
#[test]
fn test_token_tree_docs() -> anyhow::Result<()> {
let syscall = sys_call::System::Yield;
assert_eq!(syscall.docs(), "");
Ok(())
}

View File

@@ -1,12 +1,11 @@
#[macro_export]
macro_rules! parser {
($input:expr) => {
Parser::new(Tokenizer::from($input.to_owned()))
Parser::new(Tokenizer::from($input))
};
}
mod blocks;
mod docs;
use super::Parser;
use super::Tokenizer;
use anyhow::Result;
@@ -33,7 +32,7 @@ fn test_declarations() -> Result<()> {
// The below line should fail
let y = 234
"#;
let tokenizer = Tokenizer::from(input.to_owned());
let tokenizer = Tokenizer::from(input);
let mut parser = Parser::new(tokenizer);
let expression = parser.parse()?.unwrap();
@@ -45,6 +44,36 @@ fn test_declarations() -> Result<()> {
Ok(())
}
#[test]
fn test_const_declaration() -> Result<()> {
let input = r#"
const item = 20c;
const decimal = 200.15;
const nameConst = "str_lit";
"#;
let tokenizer = Tokenizer::from(input);
let mut parser = Parser::new(tokenizer);
assert_eq!(
"(const item = 293.15)",
parser.parse()?.unwrap().to_string()
);
assert_eq!(
"(const decimal = 200.15)",
parser.parse()?.unwrap().to_string()
);
assert_eq!(
r#"(const nameConst = "str_lit")"#,
parser.parse()?.unwrap().to_string()
);
assert_eq!(None, parser.parse()?);
Ok(())
}
#[test]
fn test_function_expression() -> Result<()> {
let input = r#"
@@ -54,7 +83,7 @@ fn test_function_expression() -> Result<()> {
}
"#;
let tokenizer = Tokenizer::from(input.to_owned());
let tokenizer = Tokenizer::from(input);
let mut parser = Parser::new(tokenizer);
let expression = parser.parse()?.unwrap();
@@ -73,7 +102,7 @@ fn test_function_invocation() -> Result<()> {
add();
"#;
let tokenizer = Tokenizer::from(input.to_owned());
let tokenizer = Tokenizer::from(input);
let mut parser = Parser::new(tokenizer);
let expression = parser.parse()?.unwrap();
@@ -89,7 +118,7 @@ fn test_priority_expression() -> Result<()> {
let x = (4);
"#;
let tokenizer = Tokenizer::from(input.to_owned());
let tokenizer = Tokenizer::from(input);
let mut parser = Parser::new(tokenizer);
let expression = parser.parse()?.unwrap();

View File

@@ -195,6 +195,18 @@ impl std::fmt::Display for LiteralOrVariable {
}
}
#[derive(Debug, PartialEq, Eq)]
pub struct ConstDeclarationExpression {
pub name: Spanned<String>,
pub value: Spanned<Literal>,
}
impl std::fmt::Display for ConstDeclarationExpression {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "(const {} = {})", self.name, self.value)
}
}
#[derive(Debug, PartialEq, Eq)]
pub struct DeviceDeclarationExpression {
/// any variable-like name
@@ -316,6 +328,7 @@ pub enum Expression {
Binary(Spanned<BinaryExpression>),
Block(Spanned<BlockExpression>),
Break(Span),
ConstDeclaration(Spanned<ConstDeclarationExpression>),
Continue(Span),
Declaration(Spanned<String>, Box<Spanned<Expression>>),
DeviceDeclaration(Spanned<DeviceDeclarationExpression>),
@@ -342,6 +355,7 @@ impl std::fmt::Display for Expression {
Expression::Binary(e) => write!(f, "{}", e),
Expression::Block(e) => write!(f, "{}", e),
Expression::Break(_) => write!(f, "break"),
Expression::ConstDeclaration(e) => write!(f, "{}", e),
Expression::Continue(_) => write!(f, "continue"),
Expression::Declaration(id, e) => write!(f, "(let {} = {})", id, e),
Expression::DeviceDeclaration(e) => write!(f, "{}", e),
@@ -362,4 +376,3 @@ impl std::fmt::Display for Expression {
}
}
}