Skip to content

Commit

Permalink
Merge pull request #51 from Ph0enixKM/A69
Browse files Browse the repository at this point in the history
A69 Null value
  • Loading branch information
Ph0enixKM authored Dec 22, 2022
2 parents 1cc5654 + 20fb806 commit f5059cc
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 22 deletions.
6 changes: 4 additions & 2 deletions src/modules/expression/binop/eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ impl SyntaxModule<ParserMetadata> for Eq {
let tok = meta.get_current_token();
token(meta, "==")?;
syntax(meta, &mut *self.right)?;
let error = "Cannot compare two values of different types";
expression_arms_of_same_type(meta, &self.left, &self.right, tok, error)?;
let l_type = self.left.get_type();
let r_type = self.right.get_type();
let message = format!("Cannot compare two values of different types '{l_type}' == '{r_type}'");
expression_arms_of_same_type(meta, &self.left, &self.right, tok, &message)?;
Ok(())
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/modules/expression/binop/ge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ impl SyntaxModule<ParserMetadata> for Ge {
let tok = meta.get_current_token();
token(meta, ">=")?;
syntax(meta, &mut *self.right)?;
let error = "Cannot compare two values of different types";
let l_type = self.left.get_type();
let r_type = self.right.get_type();
let message = format!("Cannot compare two values of different types '{l_type}' >= '{r_type}'");
let predicate = |kind| matches!(kind, Type::Num);
expression_arms_of_type(meta, &l_type, &r_type, predicate, tok, error)?;
expression_arms_of_type(meta, &l_type, &r_type, predicate, tok, &message)?;
Ok(())
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/modules/expression/binop/gt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ impl SyntaxModule<ParserMetadata> for Gt {
let tok = meta.get_current_token();
token(meta, ">")?;
syntax(meta, &mut *self.right)?;
let error = "Cannot compare two values of different types";
let l_type = self.left.get_type();
let r_type = self.right.get_type();
let message = format!("Cannot compare two values of different types '{l_type}' > '{r_type}'");
let predicate = |kind| matches!(kind, Type::Num);
expression_arms_of_type(meta, &l_type, &r_type, predicate, tok, error)?;
expression_arms_of_type(meta, &l_type, &r_type, predicate, tok, &message)?;
Ok(())
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/modules/expression/binop/le.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ impl SyntaxModule<ParserMetadata> for Le {
let tok = meta.get_current_token();
token(meta, "<=")?;
syntax(meta, &mut *self.right)?;
let error = "Cannot compare two values of different types";
let l_type = self.left.get_type();
let r_type = self.right.get_type();
let message = format!("Cannot compare two values of different types '{l_type}' <= '{r_type}'");
let predicate = |kind| matches!(kind, Type::Num);
expression_arms_of_type(meta, &l_type, &r_type, predicate, tok, error)?;
expression_arms_of_type(meta, &l_type, &r_type, predicate, tok, &message)?;
Ok(())
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/modules/expression/binop/lt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ impl SyntaxModule<ParserMetadata> for Lt {
let tok = meta.get_current_token();
token(meta, "<")?;
syntax(meta, &mut *self.right)?;
let error = "Cannot compare two values of different types";
let l_type = self.left.get_type();
let r_type = self.right.get_type();
let message = format!("Cannot compare two values of different types '{l_type}' < '{r_type}'");
let predicate = |kind| matches!(kind, Type::Num);
expression_arms_of_type(meta, &l_type, &r_type, predicate, tok, error)?;
expression_arms_of_type(meta, &l_type, &r_type, predicate, tok, &message)?;
Ok(())
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/modules/expression/binop/neq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ impl SyntaxModule<ParserMetadata> for Neq {
let tok = meta.get_current_token();
token(meta, "!=")?;
syntax(meta, &mut *self.right)?;
let error = "Cannot compare two values of different types";
expression_arms_of_same_type(meta, &self.left, &self.right, tok, error)?;
let l_type = self.left.get_type();
let r_type = self.right.get_type();
let message = format!("Cannot compare two values of different types '{l_type}' != '{r_type}'");
expression_arms_of_same_type(meta, &self.left, &self.right, tok, &message)?;
Ok(())
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/modules/expression/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use super::literal::{
number::Number,
text::Text,
array::Array,
range::Range
range::Range,
null::Null
};
use super::binop::{
add::Add,
Expand Down Expand Up @@ -59,7 +60,8 @@ pub enum ExprType {
Ternary(Ternary),
FunctionInvocation(FunctionInvocation),
Array(Array),
Range(Range)
Range(Range),
Null(Null)
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -101,7 +103,7 @@ impl Expr {
// Arithmetic operators
Add, Sub, Mul, Div, Modulo,
// Literals
Range, Parenthesis, CommandExpr, Bool, Number, Text, Array,
Range, Parenthesis, CommandExpr, Bool, Number, Text, Array, Null,
// Function invocation
FunctionInvocation,
// Variable access
Expand Down
6 changes: 6 additions & 0 deletions src/modules/expression/literal/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ impl SyntaxModule<ParserMetadata> for Array {
// Try to parse array type
match try_parse_type(meta) {
Ok(kind) => {
if matches!(kind, Type::Array(_)) {
return error!(meta, tok, "Arrays cannot be nested due to the Bash limitations")
}
self.kind = Type::Array(Box::new(kind));
token(meta, "]")?;
},
Expand All @@ -44,6 +47,9 @@ impl SyntaxModule<ParserMetadata> for Array {
Err(Failure::Quiet(_)) => {
loop {
let tok = meta.get_current_token();
if let Ok(_) = token(meta, "[") {
return error!(meta, tok, "Arrays cannot be nested due to the Bash limitations")
}
// Parse array value
let mut value = Expr::new();
syntax(meta, &mut value)?;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/expression/literal/null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use heraclitus_compiler::prelude::*;
use crate::{utils::{ParserMetadata, TranslateMetadata}, modules::types::{Type, Typed}};
use crate::translate::module::TranslateModule;

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Null {}

impl Typed for Null {
Expand Down
20 changes: 14 additions & 6 deletions src/modules/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,18 @@ pub fn try_parse_type(meta: &mut ParserMetadata) -> Result<Type, Failure> {
Ok(Type::Null)
},
"[" => {
let index = meta.get_index();
meta.increment_index();
let t = parse_type(meta)?;
token(meta, "]")?;
match t {
Type::Array(_) => error!(meta, tok, "Arrays cannot be nested due to the Bash limitations"),
_ => Ok(Type::Array(Box::new(t))),
match try_parse_type(meta) {
Ok(Type::Array(_)) => error!(meta, tok, "Arrays cannot be nested due to the Bash limitations"),
Ok(result_type) => {
token(meta, "]")?;
Ok(Type::Array(Box::new(result_type)))
},
Err(_) => {
meta.set_index(index);
Err(Failure::Quiet(PositionInfo::at_eof(meta)))
}
}
},
// Error messages to help users of other languages understand the syntax
Expand All @@ -90,6 +96,8 @@ pub fn try_parse_type(meta: &mut ParserMetadata) -> Result<Type, Failure> {
_ => Err(Failure::Quiet(PositionInfo::at_eof(meta)))
}
},
None => Err(Failure::Quiet(PositionInfo::at_eof(meta)))
None => {
Err(Failure::Quiet(PositionInfo::at_eof(meta)))
}
}
}
9 changes: 9 additions & 0 deletions src/tests/validity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,3 +628,12 @@ fn range_loop_inclusive() {
";
test_amber!(code, "0\n1\n2\n3\n4\n5");
}

#[test]
fn null() {
let code = "
let a = null
echo a
";
test_amber!(code, "");
}

0 comments on commit f5059cc

Please sign in to comment.