Skip to content

Commit

Permalink
fix: constant variables are usable now
Browse files Browse the repository at this point in the history
  • Loading branch information
Ph0enixKM committed Dec 5, 2024
1 parent 82817af commit 2540c63
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 100 deletions.
6 changes: 2 additions & 4 deletions src/modules/statement/stmt.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use heraclitus_compiler::prelude::*;
use itertools::Itertools;
use crate::docs::module::DocumentationModule;
use crate::modules::variable::constinit::ConstInit;
use crate::utils::metadata::{ParserMetadata, TranslateMetadata};
use crate::modules::expression::expr::{Expr, ExprType};
use crate::translate::module::TranslateModule;
Expand Down Expand Up @@ -71,8 +70,7 @@ pub enum StatementType {
Exit(Exit),
CommandModifier(CommandModifier),
Comment(Comment),
CommentDoc(CommentDoc),
ConstInit(ConstInit),
CommentDoc(CommentDoc)
}

#[derive(Debug, Clone)]
Expand All @@ -91,7 +89,7 @@ impl Statement {
// Conditions
IfChain, IfCondition,
// Variables
VariableInit, VariableSet, ConstInit,
VariableInit, VariableSet,
// Short hand
ShorthandAdd, ShorthandSub,
ShorthandMul, ShorthandDiv,
Expand Down
73 changes: 0 additions & 73 deletions src/modules/variable/constinit.rs

This file was deleted.

21 changes: 14 additions & 7 deletions src/modules/variable/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@ pub struct VariableInit {
name: String,
expr: Box<Expr>,
global_id: Option<usize>,
is_fun_ctx: bool
is_fun_ctx: bool,
is_const: bool
}

impl VariableInit {
fn handle_add_variable(&mut self, meta: &mut ParserMetadata, name: &str, kind: Type, tok: Option<Token>) -> SyntaxResult {
handle_identifier_name(meta, name, tok)?;
self.global_id = meta.add_var(name, kind, false);
fn handle_add_variable(
&mut self,
meta: &mut ParserMetadata,
tok: Option<Token>
) -> SyntaxResult {
handle_identifier_name(meta, &self.name, tok)?;
self.global_id = meta.add_var(&self.name, self.expr.get_type(), self.is_const);
Ok(())
}
}
Expand All @@ -30,20 +35,22 @@ impl SyntaxModule<ParserMetadata> for VariableInit {
name: String::new(),
expr: Box::new(Expr::new()),
global_id: None,
is_fun_ctx: false
is_fun_ctx: false,
is_const: false
}
}

fn parse(&mut self, meta: &mut ParserMetadata) -> SyntaxResult {
token(meta, "let")?;
let keyword = token_by(meta, |word| ["let", "const"].contains(&word.as_str()))?;
self.is_const = keyword == "const";
// Get the variable name
let tok = meta.get_current_token();
self.name = variable(meta, variable_name_extensions())?;
context!({
token(meta, "=")?;
syntax(meta, &mut *self.expr)?;
// Add a variable to the memory
self.handle_add_variable(meta, &self.name.clone(), self.expr.get_type(), tok)?;
self.handle_add_variable(meta, tok)?;
self.is_fun_ctx = meta.context.is_fun_ctx;
Ok(())
}, |position| {
Expand Down
1 change: 0 additions & 1 deletion src/modules/variable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use super::expression::expr::Expr;
pub mod init;
pub mod set;
pub mod get;
pub mod constinit;

pub fn variable_name_extensions() -> Vec<char> {
vec!['_']
Expand Down
15 changes: 0 additions & 15 deletions src/tests/validity/constant.ab

This file was deleted.

22 changes: 22 additions & 0 deletions src/tests/validity/variable_constant_condition.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Output
// Outer: 42
// If: 12
// Else: 24

main {
const x = 42
echo "Outer: {x}"

if x > 12 {
const x = 12
echo "If: {x}"
}

if x < 12 {
const x = 12
echo "If: {x}"
} else {
const x = 24
echo "Else: {x}"
}
}
14 changes: 14 additions & 0 deletions src/tests/validity/variable_constant_function.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Output
// 12
// 42

const x = 42

fun foo() {
const test = 12
echo test
}

foo()

echo x
15 changes: 15 additions & 0 deletions src/tests/validity/variable_constant_loop.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Output
// 42
// 1
// 2
// 3

main {
const x = 42
echo x

for i in [1, 2, 3] {
const x = i
echo x
}
}

0 comments on commit 2540c63

Please sign in to comment.