1
0

Begin adding maps

This commit is contained in:
Jeff 2024-08-09 05:18:39 -04:00
parent d0dba35285
commit 55a8661618
6 changed files with 63 additions and 18 deletions

View File

@ -36,7 +36,7 @@ impl<T: Display> Display for Node<T> {
pub enum Statement { pub enum Statement {
// Top-level statements // Top-level statements
Assignment { Assignment {
identifier: Identifier, identifier: Node<Identifier>,
value_node: Box<Node<Statement>>, value_node: Box<Node<Statement>>,
}, },

View File

@ -80,7 +80,9 @@ impl<'a> Analyzer<'a> {
Statement::Assignment { Statement::Assignment {
value_node: value, .. value_node: value, ..
} => { } => {
if let None = value.inner.expected_type(self.variables) { self.analyze_node(value)?;
if value.inner.expected_type(self.variables).is_none() {
return Err(AnalyzerError::ExpectedValue { return Err(AnalyzerError::ExpectedValue {
actual: value.as_ref().clone(), actual: value.as_ref().clone(),
position: value.position, position: value.position,

View File

@ -189,6 +189,16 @@ impl Lexer {
(Token::Less, (self.position - 1, self.position)) (Token::Less, (self.position - 1, self.position))
} }
} }
'{' => {
self.position += 1;
(Token::LeftCurlyBrace, (self.position - 1, self.position))
}
'}' => {
self.position += 1;
(Token::RightCurlyBrace, (self.position - 1, self.position))
}
_ => { _ => {
self.position += 1; self.position += 1;
@ -415,6 +425,27 @@ impl From<ParseIntError> for LexError {
mod tests { mod tests {
use super::*; use super::*;
#[test]
fn map() {
let input = "{ x = 42, y = 'foobar' }";
assert_eq!(
lex(input),
Ok(vec![
(Token::LeftCurlyBrace, (0, 1)),
(Token::Identifier("x"), (2, 3)),
(Token::Equal, (4, 5)),
(Token::Integer(42), (6, 8)),
(Token::Comma, (8, 9)),
(Token::Identifier("y"), (10, 11)),
(Token::Equal, (12, 13)),
(Token::String("foobar"), (14, 22)),
(Token::RightCurlyBrace, (23, 24)),
(Token::Eof, (24, 24)),
])
)
}
#[test] #[test]
fn greater_than() { fn greater_than() {
let input = ">"; let input = ">";

View File

@ -27,16 +27,16 @@ use crate::{
/// Ok(AbstractSyntaxTree { /// Ok(AbstractSyntaxTree {
/// nodes: [ /// nodes: [
/// Node { /// Node {
/// inner: Statement::Assign( /// inner: Statement::Assignment {
/// Box::new(Node { /// identifier: Node {
/// inner: Statement::Identifier("x".into()), /// inner: Identifier::new("x"),
/// position: (0, 1), /// position: (0, 1),
/// }), /// },
/// Box::new(Node { /// value_node: Box::new(Node {
/// inner: Statement::Constant(Value::integer(42)), /// inner: Statement::Constant(Value::integer(42)),
/// position: (4, 6), /// position: (4, 6),
/// }) /// })
/// ), /// },
/// position: (0, 6), /// position: (0, 6),
/// } /// }
/// ].into(), /// ].into(),
@ -86,16 +86,16 @@ pub fn parse(input: &str) -> Result<AbstractSyntaxTree, ParseError> {
/// nodes, /// nodes,
/// Into::<VecDeque<Node<Statement>>>::into([ /// Into::<VecDeque<Node<Statement>>>::into([
/// Node { /// Node {
/// inner: Statement::Assign( /// inner: Statement::Assignment {
/// Box::new(Node { /// identifier: Node {
/// inner: Statement::Identifier("x".into()), /// inner: Identifier::new("x"),
/// position: (0, 1), /// position: (0, 1),
/// }), /// },
/// Box::new(Node { /// value_node: Box::new(Node {
/// inner: Statement::Constant(Value::integer(42)), /// inner: Statement::Constant(Value::integer(42)),
/// position: (4, 6), /// position: (4, 6),
/// }) /// }),
/// ), /// },
/// position: (0, 6), /// position: (0, 6),
/// } /// }
/// ]), /// ]),
@ -182,7 +182,7 @@ impl<'src> Parser<'src> {
return Ok(Node::new( return Ok(Node::new(
Statement::Assignment { Statement::Assignment {
identifier, identifier: Node::new(identifier, left_node.position),
value_node: Box::new(right_node), value_node: Box::new(right_node),
}, },
(left_start, right_end), (left_start, right_end),
@ -964,7 +964,7 @@ mod tests {
Ok(AbstractSyntaxTree { Ok(AbstractSyntaxTree {
nodes: [Node::new( nodes: [Node::new(
Statement::Assignment { Statement::Assignment {
identifier: Identifier::new("a"), identifier: Node::new(Identifier::new("a"), (0, 1)),
value_node: Box::new(Node::new( value_node: Box::new(Node::new(
Statement::BinaryOperation { Statement::BinaryOperation {
left: Box::new(Node::new( left: Box::new(Node::new(

View File

@ -31,12 +31,14 @@ pub enum Token<'src> {
Equal, Equal,
Greater, Greater,
GreaterEqual, GreaterEqual,
LeftCurlyBrace,
LeftParenthesis, LeftParenthesis,
LeftSquareBrace, LeftSquareBrace,
Less, Less,
LessEqual, LessEqual,
Minus, Minus,
Plus, Plus,
RightCurlyBrace,
RightParenthesis, RightParenthesis,
RightSquareBrace, RightSquareBrace,
Star, Star,
@ -59,6 +61,7 @@ impl<'src> Token<'src> {
Token::Integer(integer) => TokenOwned::Integer(*integer), Token::Integer(integer) => TokenOwned::Integer(*integer),
Token::IsEven => TokenOwned::IsEven, Token::IsEven => TokenOwned::IsEven,
Token::IsOdd => TokenOwned::IsOdd, Token::IsOdd => TokenOwned::IsOdd,
Token::LeftCurlyBrace => TokenOwned::LeftCurlyBrace,
Token::LeftParenthesis => TokenOwned::LeftParenthesis, Token::LeftParenthesis => TokenOwned::LeftParenthesis,
Token::LeftSquareBrace => TokenOwned::LeftSquareBrace, Token::LeftSquareBrace => TokenOwned::LeftSquareBrace,
Token::Length => TokenOwned::Length, Token::Length => TokenOwned::Length,
@ -67,6 +70,7 @@ impl<'src> Token<'src> {
Token::Minus => TokenOwned::Minus, Token::Minus => TokenOwned::Minus,
Token::Plus => TokenOwned::Plus, Token::Plus => TokenOwned::Plus,
Token::ReadLine => TokenOwned::ReadLine, Token::ReadLine => TokenOwned::ReadLine,
Token::RightCurlyBrace => TokenOwned::RightCurlyBrace,
Token::RightParenthesis => TokenOwned::RightParenthesis, Token::RightParenthesis => TokenOwned::RightParenthesis,
Token::RightSquareBrace => TokenOwned::RightSquareBrace, Token::RightSquareBrace => TokenOwned::RightSquareBrace,
Token::Star => TokenOwned::Star, Token::Star => TokenOwned::Star,
@ -91,6 +95,7 @@ impl<'src> Token<'src> {
Token::Integer(_) => "integer", Token::Integer(_) => "integer",
Token::IsEven => "is_even", Token::IsEven => "is_even",
Token::IsOdd => "is_odd", Token::IsOdd => "is_odd",
Token::LeftCurlyBrace => "{",
Token::LeftParenthesis => "(", Token::LeftParenthesis => "(",
Token::LeftSquareBrace => "[", Token::LeftSquareBrace => "[",
Token::Length => "length", Token::Length => "length",
@ -99,6 +104,7 @@ impl<'src> Token<'src> {
Token::Minus => "-", Token::Minus => "-",
Token::Plus => "+", Token::Plus => "+",
Token::ReadLine => "read_line", Token::ReadLine => "read_line",
Token::RightCurlyBrace => "}",
Token::RightParenthesis => ")", Token::RightParenthesis => ")",
Token::RightSquareBrace => "]", Token::RightSquareBrace => "]",
Token::Star => "*", Token::Star => "*",
@ -132,6 +138,7 @@ impl<'src> PartialEq for Token<'src> {
(Token::Integer(left), Token::Integer(right)) => left == right, (Token::Integer(left), Token::Integer(right)) => left == right,
(Token::IsEven, Token::IsEven) => true, (Token::IsEven, Token::IsEven) => true,
(Token::IsOdd, Token::IsOdd) => true, (Token::IsOdd, Token::IsOdd) => true,
(Token::LeftCurlyBrace, Token::LeftCurlyBrace) => true,
(Token::LeftParenthesis, Token::LeftParenthesis) => true, (Token::LeftParenthesis, Token::LeftParenthesis) => true,
(Token::LeftSquareBrace, Token::LeftSquareBrace) => true, (Token::LeftSquareBrace, Token::LeftSquareBrace) => true,
(Token::Length, Token::Length) => true, (Token::Length, Token::Length) => true,
@ -140,6 +147,7 @@ impl<'src> PartialEq for Token<'src> {
(Token::Minus, Token::Minus) => true, (Token::Minus, Token::Minus) => true,
(Token::Plus, Token::Plus) => true, (Token::Plus, Token::Plus) => true,
(Token::ReadLine, Token::ReadLine) => true, (Token::ReadLine, Token::ReadLine) => true,
(Token::RightCurlyBrace, Token::RightCurlyBrace) => true,
(Token::RightParenthesis, Token::RightParenthesis) => true, (Token::RightParenthesis, Token::RightParenthesis) => true,
(Token::RightSquareBrace, Token::RightSquareBrace) => true, (Token::RightSquareBrace, Token::RightSquareBrace) => true,
(Token::Star, Token::Star) => true, (Token::Star, Token::Star) => true,
@ -180,12 +188,14 @@ pub enum TokenOwned {
Equal, Equal,
Greater, Greater,
GreaterOrEqual, GreaterOrEqual,
LeftCurlyBrace,
LeftParenthesis, LeftParenthesis,
LeftSquareBrace, LeftSquareBrace,
Less, Less,
LessOrEqual, LessOrEqual,
Minus, Minus,
Plus, Plus,
RightCurlyBrace,
RightParenthesis, RightParenthesis,
RightSquareBrace, RightSquareBrace,
Star, Star,
@ -208,6 +218,7 @@ impl Display for TokenOwned {
TokenOwned::Integer(integer) => write!(f, "{integer}"), TokenOwned::Integer(integer) => write!(f, "{integer}"),
TokenOwned::IsEven => Token::IsEven.fmt(f), TokenOwned::IsEven => Token::IsEven.fmt(f),
TokenOwned::IsOdd => Token::IsOdd.fmt(f), TokenOwned::IsOdd => Token::IsOdd.fmt(f),
TokenOwned::LeftCurlyBrace => Token::LeftCurlyBrace.fmt(f),
TokenOwned::LeftParenthesis => Token::LeftParenthesis.fmt(f), TokenOwned::LeftParenthesis => Token::LeftParenthesis.fmt(f),
TokenOwned::LeftSquareBrace => Token::LeftSquareBrace.fmt(f), TokenOwned::LeftSquareBrace => Token::LeftSquareBrace.fmt(f),
TokenOwned::Length => Token::Length.fmt(f), TokenOwned::Length => Token::Length.fmt(f),
@ -216,6 +227,7 @@ impl Display for TokenOwned {
TokenOwned::Minus => Token::Minus.fmt(f), TokenOwned::Minus => Token::Minus.fmt(f),
TokenOwned::Plus => Token::Plus.fmt(f), TokenOwned::Plus => Token::Plus.fmt(f),
TokenOwned::ReadLine => Token::ReadLine.fmt(f), TokenOwned::ReadLine => Token::ReadLine.fmt(f),
TokenOwned::RightCurlyBrace => Token::RightCurlyBrace.fmt(f),
TokenOwned::RightParenthesis => Token::RightParenthesis.fmt(f), TokenOwned::RightParenthesis => Token::RightParenthesis.fmt(f),
TokenOwned::RightSquareBrace => Token::RightSquareBrace.fmt(f), TokenOwned::RightSquareBrace => Token::RightSquareBrace.fmt(f),
TokenOwned::Star => Token::Star.fmt(f), TokenOwned::Star => Token::Star.fmt(f),

View File

@ -65,7 +65,7 @@ impl Vm {
}); });
}; };
variables.insert(identifier, value); variables.insert(identifier.inner, value);
Ok(None) Ok(None)
} }