Skip to content

Commit 8b59a74

Browse files
authored
feat: AST generation for compiler builtins convDate and isZero (#31)
**Description** This PR adds AST generation for `convDate` and `isZero` For convDate - convDate: is implemented by getting the date: DD-MM-YYYY we have to convert it to YYYYMMDD - If we treat each part of the date as an integer - YYYY * 1000 will give us: YYYY0000 - MM * 100 will give us: MM00 - DD * 1 will give us DD - Adding up all these values will give us YYYYMMDD For isZero - isZero is implmented by after generating the AST for the expression inside isZero creating a leaf with a 0 value and creating a node with comparison "==" with the expression as the left_child and the 0 as the right_child
1 parent 1c7b872 commit 8b59a74

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/compiler/ast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ pub enum AstPtr {
5050
While,
5151
Read,
5252
Write,
53+
ConvDate,
5354
}
5455

5556
pub enum AstNodeRef {

src/grammar/rules_actions.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,13 @@ pub fn function_is_zero_function_is_zero_call(
494494
compiler_context.write_to_parser_file(&format!(
495495
"<FunctionIsZero> -> {token_is_zero} {token_par_open} <E> {token_par_close}"
496496
));
497+
let zero_leaf = Rc::new(Node::new_leaf(NodeValue::Value("0".into())));
498+
compiler_context.ast.create_node(
499+
AstAction::EQ,
500+
AstPtr::ArithmeticExpression.into(),
501+
zero_leaf.into(),
502+
AstPtr::IsZero,
503+
);
497504
FunctionIsZero {
498505
token_is_zero,
499506
token_par_open,
@@ -514,6 +521,46 @@ pub fn function_conv_date_function_conv_date_variable_call(
514521
compiler_context.write_to_parser_file(&format!(
515522
"<FunctionConvDate> -> {token_conv_date} {token_par_open} {token_date} {token_par_close}"
516523
));
524+
let thousand_leaf = Rc::new(Node::new_leaf(NodeValue::Value("1000".into())));
525+
let hundread_leaf = Rc::new(Node::new_leaf(NodeValue::Value("100".into())));
526+
let one_leaf = Rc::new(Node::new_leaf(NodeValue::Value("1".into())));
527+
528+
let year_leaf = Rc::new(Node::new_leaf(NodeValue::Value(token_date.year.clone())));
529+
let month_leaf = Rc::new(Node::new_leaf(NodeValue::Value(token_date.month.clone())));
530+
let day_leaf = Rc::new(Node::new_leaf(NodeValue::Value(token_date.day.clone())));
531+
532+
let year_node = compiler_context.ast.create_node(
533+
AstAction::Mult,
534+
year_leaf.into(),
535+
thousand_leaf.into(),
536+
AstPtr::ConvDate,
537+
);
538+
let month_node = compiler_context.ast.create_node(
539+
AstAction::Mult,
540+
month_leaf.into(),
541+
hundread_leaf.into(),
542+
AstPtr::ConvDate,
543+
);
544+
let day_node = compiler_context.ast.create_node(
545+
AstAction::Mult,
546+
day_leaf.into(),
547+
one_leaf.into(),
548+
AstPtr::ConvDate,
549+
);
550+
551+
let sum_year_month_node = compiler_context.ast.create_node(
552+
AstAction::Plus,
553+
year_node.into(),
554+
month_node.into(),
555+
AstPtr::ConvDate,
556+
);
557+
compiler_context.ast.create_node(
558+
AstAction::Plus,
559+
sum_year_month_node.into(),
560+
day_node.into(),
561+
AstPtr::ConvDate,
562+
);
563+
517564
FunctionConvDate {
518565
token_conv_date,
519566
token_par_open,
@@ -751,6 +798,13 @@ pub fn assignment_assignment_conv_date(
751798
compiler_context.write_to_parser_file(&format!(
752799
"<Assignment> -> {token_id} {token_assign} <FunctionConvDate>"
753800
));
801+
let leaf = Rc::new(Node::new_leaf(NodeValue::Value(token_id.clone())));
802+
compiler_context.ast.create_node(
803+
AstAction::Assign,
804+
leaf.into(),
805+
AstPtr::ConvDate.into(),
806+
AstPtr::Assignment,
807+
);
754808
Assignment::AssignmentConvDate(ConvDate {
755809
token_id,
756810
token_assign,

0 commit comments

Comments
 (0)