Skip to content

Commit

Permalink
feat: add a iota keyword to create distinct constants
Browse files Browse the repository at this point in the history
  • Loading branch information
delehef committed Sep 15, 2023
1 parent ec6f338 commit 1f3a820
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/compiler/compiletime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use anyhow::*;
use super::{
generator::{make_ast_error, reduce},
tables::Scope,
Ast, AstNode, CompileSettings, Token,
Ast, AstNode, CompileSettings, Node, Token,
};

fn compile_time_constants(e: &AstNode, ctx: &mut Scope, settings: &CompileSettings) -> Result<()> {
Expand All @@ -15,7 +15,15 @@ fn compile_time_constants(e: &AstNode, ctx: &mut Scope, settings: &CompileSettin
}
Token::DefConsts(cs) => {
for (name, exp) in cs.iter() {
let value = reduce(exp, ctx, settings)?.unwrap();
let value = match &exp.class {
// If the constant value is iota, assign it to a deterministic pseudo-random value
Token::Symbol(x) if ["iota", "ι", "ɩ"].contains(&x.as_str()) => {
let mut hasher = DefaultHasher::new();
std::hash::Hash::hash(&name, &mut hasher);
Node::from_const((std::hash::Hasher::finish(&hasher) >> 1) as isize)
}
_ => reduce(exp, ctx, settings)?.unwrap(),
};
ctx.insert_constant(
name,
value.pure_eval().with_context(|| make_ast_error(exp))?,
Expand Down

0 comments on commit 1f3a820

Please sign in to comment.