Compare commits

...

2 Commits

Author SHA1 Message Date
bog a0b8cda00d string literal. 2024-03-01 17:06:10 +01:00
bog efdbafb645 float and int values. 2024-03-01 15:34:45 +01:00
6 changed files with 279 additions and 25 deletions

View File

@ -1,4 +1,8 @@
MODULE ::= INSTR*
INSTR ::= EXPR semicolon
EXPR ::= BUILTIN
BUILTIN ::= bool
BUILTIN ::=
| bool
| float
| int
| string

View File

@ -1,8 +1,8 @@
use crate::ast::node;
use crate::ast::node::Node;
struct Token {
position: usize,
value: String,
value: String
}
pub struct Lexer {
@ -53,6 +53,128 @@ impl Lexer {
false
}
fn scan_string(&mut self) -> Option<Token> {
let mut value : String;
let mut length = 1;
if let Some('"') = self.text.chars().nth(self.cursor) {
value = String::from("");
} else {
return None;
}
let mut escape = false;
let mut closed = false;
for c in self.text.chars().skip(length + self.cursor) {
if !escape && c == '\\' {
escape = true;
length += 1;
continue;
}
if escape {
match c {
'"' => value.push('"'),
'\\' => value.push('\\'),
'n' => value.push('\n'),
't' => value.push('\t'),
'r' => value.push('\r'),
_ => {}
}
} else if c == '"' {
closed = true;
break;
} else {
value.push(c);
}
escape = false;
length += 1;
}
if length >= 2 && closed {
return Some(Token {
position: 1 + self.cursor + length,
value
});
}
None
}
fn scan_int(&self) -> Option<Token> {
let mut value = String::from("");
if let Some('-') = self.text.chars().nth(self.cursor) {
value.push('-');
}
for c in self.text.chars().skip(self.cursor + value.len()) {
if c.is_digit(10) {
value.push(c);
} else {
break;
}
}
if value.len() > 0 {
Some(Token {
position: self.cursor + value.len(),
value
})
} else {
None
}
}
fn scan_float(&self) -> Option<Token> {
let mut value = String::from("");
if let Some('-') = self.text.chars().nth(self.cursor) {
value.push('-');
}
for c in self.text.chars().skip(self.cursor + value.len()) {
if c.is_digit(10) {
value.push(c);
} else {
break;
}
}
if let Some('.') = self.text.chars().nth(self.cursor + value.len()) {
value.push('.');
} else {
return None;
}
for c in self.text.chars().skip(self.cursor + value.len()) {
if c.is_digit(10) {
value.push(c);
} else {
break;
}
}
if value.len() > 0 && value != "." {
if value.chars().nth(0).unwrap() == '.' {
value = format!("0{}", value);
}
if value.chars().last().unwrap() == '.' {
value = format!("{}0", value);
}
Some(Token {
position: self.cursor + value.len(),
value
})
} else {
None
}
}
fn scan_text(&self, text: &str) -> Option<Token> {
let c = self.cursor;
@ -112,24 +234,43 @@ impl Lexer {
}
impl Iterator for Lexer {
type Item = node::Node;
type Item = Node;
fn next(&mut self) -> Option<Self::Item> {
self.skip_spaces();
if let Some(token) = self.scan_keyword("true") {
self.cursor = token.position;
return Some(node::Node::Bool(token.value == "true"));
return Some(Node::Bool(token.value == "true"));
}
if let Some(token) = self.scan_keyword("false") {
self.cursor = token.position;
return Some(node::Node::Bool(token.value == "true"));
return Some(Node::Bool(token.value == "true"));
}
if let Some(token) = self.scan_text(";") {
self.cursor = token.position;
return Some(node::Node::Semicolon);
return Some(Node::Semicolon);
}
if let Some(token) = self.scan_string() {
self.cursor = token.position;
return Some(Node::String(token.value.clone()));
}
if let Some(token) = self.scan_float() {
if let Ok(val) = token.value.parse::<f64>() {
self.cursor = token.position;
return Some(Node::Float(val));
}
}
if let Some(token) = self.scan_int() {
if let Ok(val) = token.value.parse::<i32>() {
self.cursor = token.position;
return Some(Node::Int(val));
}
}
None
@ -140,8 +281,9 @@ impl Iterator for Lexer {
mod test {
use super::*;
use crate::ast::error;
use crate::ast::node::Node;
fn lexer_run(lexer: &mut Lexer, text: &str) -> Result<Vec<node::Node>, error::AstError> {
fn lexer_run(lexer: &mut Lexer, text: &str) -> Result<Vec<Node>, error::AstError> {
lexer.prepare(text);
let result = lexer.collect();
@ -168,8 +310,8 @@ mod test {
let mut lex = Lexer::new();
let res = lexer_run(&mut lex, "true false").unwrap();
assert_eq!(2, res.len());
assert_eq!("Bool(true)", format!("{:?}", res.get(0).unwrap()));
assert_eq!("Bool(false)", format!("{:?}", res.get(1).unwrap()));
assert_eq!(Node::Bool(true), *res.get(0).unwrap());
assert_eq!(Node::Bool(false), *res.get(1).unwrap());
}
#[test]
@ -177,8 +319,8 @@ mod test {
let mut lex = Lexer::new();
let res = lexer_run(&mut lex, "true;").unwrap();
assert_eq!(2, res.len());
assert_eq!("Bool(true)", format!("{:?}", res.get(0).unwrap()));
assert_eq!("Semicolon", format!("{:?}", res.get(1).unwrap()));
assert_eq!(Node::Bool(true), *res.get(0).unwrap());
assert_eq!(Node::Semicolon, *res.get(1).unwrap());
}
#[test]
@ -203,8 +345,47 @@ mod test {
let mut lex = Lexer::new();
let res = lexer_run(&mut lex, " ;; ")?;
assert_eq!(2, res.len());
assert_eq!("Semicolon", format!("{:?}", res.get(0).unwrap()));
assert_eq!("Semicolon", format!("{:?}", res.get(1).unwrap()));
assert_eq!(Node::Semicolon, *res.get(0).unwrap());
assert_eq!(Node::Semicolon, *res.get(1).unwrap());
Ok(())
}
#[test]
fn test_integers() -> Result<(), error::AstError> {
let mut lex = Lexer::new();
let res = lexer_run(&mut lex, " 4 -2 328")?;
assert_eq!(3, res.len());
assert_eq!(Node::Int(4), *res.get(0).unwrap());
assert_eq!(Node::Int(-2), *res.get(1).unwrap());
assert_eq!(Node::Int(328), *res.get(2).unwrap());
Ok(())
}
#[test]
fn test_floats() -> Result<(), error::AstError> {
let mut lex = Lexer::new();
let res = lexer_run(&mut lex, " 2.0 .7 5. -3.14 ")?;
assert_eq!(4, res.len());
assert_eq!(Node::Float(2.0), *res.get(0).unwrap());
assert_eq!(Node::Float(0.7), *res.get(1).unwrap());
assert_eq!(Node::Float(5.0), *res.get(2).unwrap());
assert_eq!(Node::Float(-3.14), *res.get(3).unwrap());
Ok(())
}
#[test]
fn test_string() -> Result<(), error::AstError> {
let mut lex = Lexer::new();
let res = lexer_run(&mut lex,
" \"s\\ta\\\"lut\\n\" ")?;
assert_eq!(1, res.len());
assert_eq!(Node::String(String::from("s\ta\"lut\n")),
*res.get(0).unwrap());
Ok(())
}
}

View File

@ -3,6 +3,9 @@
pub enum Node {
Module(String, Vec<Node>),
Bool(bool),
Int(i32),
Float(f64),
String(String),
Semicolon
}

View File

@ -1,5 +1,5 @@
use crate::ast::{
node,
node::Node,
lexer,
error
};
@ -9,7 +9,7 @@ pub struct Parser {
lexer: lexer::Lexer
}
type ParseResult = Result<Option<node::Node>, error::AstError>;
type ParseResult = Result<Option<Node>, error::AstError>;
impl Parser {
pub fn new(module_name: &str, lex: lexer::Lexer) -> Self {
@ -24,7 +24,7 @@ impl Parser {
}
fn parse_module(&mut self) -> ParseResult {
let mut children: Vec<node::Node> = vec![];
let mut children: Vec<Node> = vec![];
loop {
match self.parse_instr() {
@ -41,7 +41,7 @@ impl Parser {
});
}
let n = node::Node::Module(self.module_name.clone(), children);
let n = Node::Module(self.module_name.clone(), children);
Ok(Some(n))
}
@ -49,7 +49,7 @@ impl Parser {
match self.parse_expr() {
Ok(Some(expr)) => {
match self.lexer.next() {
Some(node::Node::Semicolon) => Ok(Some(expr)),
Some(Node::Semicolon) => Ok(Some(expr)),
None => Ok(None),
_ => Err(error::AstError {
msg: String::from("missing semicolon"),
@ -68,8 +68,17 @@ impl Parser {
fn parse_builtin(&mut self) -> ParseResult {
match self.lexer.next() {
Some(node::Node::Bool(value))
=> Ok(Some(node::Node::Bool(value))),
Some(Node::Int(value))
=> Ok(Some(Node::Int(value))),
Some(Node::Float(value))
=> Ok(Some(Node::Float(value))),
Some(Node::Bool(value))
=> Ok(Some(Node::Bool(value))),
Some(Node::String(value))
=> Ok(Some(Node::String(value))),
Some(other) => {
Err(error::AstError {
@ -86,11 +95,10 @@ impl Parser {
#[cfg(test)]
mod test {
use super::*;
use crate::ast::node::Node;
type TestResult = Result<(), String>;
fn test_parser(oracle: node::Node, source: &str) -> TestResult {
fn test_parser(oracle: Node, source: &str) -> TestResult {
let mut lex = lexer::Lexer::new();
lex.prepare(source);
let mut parser = Parser::new("mod", lex);
@ -140,4 +148,35 @@ mod test {
Ok(())
}
#[test]
fn numbers() -> TestResult {
test_parser(
Node::Module("mod".to_owned(), vec![
Node::Float(27.3)
]),
" 27.3; "
)?;
test_parser(
Node::Module("mod".to_owned(), vec![
Node::Int(-27)
]),
" -27; "
)?;
Ok(())
}
#[test]
fn strings() -> TestResult {
test_parser(
Node::Module("mod".to_owned(), vec![
Node::String(String::from("pizza! "))
]),
" \"pizza! \"; "
)?;
Ok(())
}
}

View File

@ -29,7 +29,12 @@ impl Evaluator {
Ok(res)
},
Node::Bool(value) => Ok(Some(Value::Bool(*value))),
Node::Int(value) => Ok(Some(Value::Int(*value))),
Node::Float(value) => Ok(Some(Value::Float(*value))),
Node::String(value) => Ok(Some(Value::String(value.clone()))),
_ => Ok(None)
}
}
@ -65,7 +70,10 @@ mod test {
Ok(())
}
} else {
Ok(())
Err(Box::new(EvalError {
msg: format!("unexpected: {:?}", value),
line: 0
}))
}
},
None => Ok(())
@ -79,4 +87,20 @@ mod test {
Ok(())
}
#[test]
fn numbers() -> TestResult {
test_eval_value(Value::Float(-271.4), " -271.4; ")?;
test_eval_value(Value::Int(333), " 333; ")?;
Ok(())
}
#[test]
fn strings() -> TestResult {
test_eval_value(Value::String(String::from(" pizzza!!!")),
" \" pizzza!!!\"; ")?;
Ok(())
}
}

View File

@ -1,6 +1,9 @@
#[derive(PartialEq)]
#[derive(Debug)]
pub enum Value {
Bool(bool)
Bool(bool),
Int(i32),
Float(f64),
String(String)
}