Make consts accessible from AbleScript code

This commit is contained in:
Alex Bethel 2021-07-13 14:22:06 -05:00
parent a225105f09
commit 3d9f847862
2 changed files with 55 additions and 21 deletions

View file

@ -1,19 +1,47 @@
// NOTE(Able): Number constants
pub const TAU: i32 = 6;
pub const PI: i32 = 3;
pub const E: i32 = 3;
pub const M: i32 = 70; // @Kev#6900's weight in kilograms
pub const PHI: i32 = 2;
pub const GOLDEN_RATIO: i32 = 2;
pub const WUA: i32 = 1;
pub const EULERS_CONSTANT: i32 = 0;
pub const GRAVITY: i32 = 10;
pub const RNG: i32 = 12; // Kixiron#5289 Randomly rolled dice
pub const STD_RNG: i32 = 4; //The standard random number is 4 (source: https://xkcd.com/221/)
pub const INF: i32 = i32::max_value();
pub const INTERESSANT: i32 = 114514; // HTGAzureX1212.#5959 interessant number
pub const FUNNY: i32 = 69; // HTGAzureX1212.#5959 funny number
pub const NEVERGONNAGIVEYOUUP: &str = "1452251871514141792252515212116";
//! Number constants.
use std::{cell::RefCell, collections::HashMap, rc::Rc};
use crate::variables::{Value, Variable};
/// Initialize a HashMap between the constant names and values
/// accessible from within AbleScript.
pub fn ablescript_consts() -> HashMap<String, Variable> {
use Value::*;
let mut map = HashMap::new();
for (name, value) in &[
("TAU", Int(6)), // Circumference / radius
("PI", Int(3)), // Deprecated, do not use
("EULER", Int(3)), // Mathematical constant e
("MASS", Int(70)), // @Kev#6900's weight in kilograms
("PHI", Int(2)), // Golden ratio
("WUA", Int(1)), // 1
("EULERS_CONSTANT", Int(0)), // ???
("GRAVITY", Int(10)), // Earth surface gravity, m/s
("RNG", Int(12)), // Kixiron#5289 Randomly rolled dice
("STD_RNG", Int(4)), // The standard random number is 4 (https://xkcd.com/221/)
("INF", Int(i32::max_value())), // The biggest number
("INTERESSANT", Int(114514)), // HTGAzureX1212.#5959 intéressant number
("FUNNY", Int(69)), // HTGAzureX1212.#5959 funny number
(
// Never gonna let you down
"NEVERGONNAGIVEYOUUP",
Str("1452251871514141792252515212116".to_owned()),
),
("OCTOTHORPE", Str("#".to_owned())), // It's an octothorpe
("ANSWER", Int(ANSWER)),
] {
map.insert(
(*name).to_owned(),
Variable {
melo: false,
value: Rc::new(RefCell::new(value.to_owned())),
},
);
}
map
}
pub const OCTOTHORPE: char = '#';
pub const ANSWER: i32 = 42;

View file

@ -20,7 +20,8 @@ use rand::random;
use crate::{
ast::{Expr, ExprKind, Iden, Stmt, StmtKind},
base_55, consts,
base_55,
consts::{self, ablescript_consts},
error::{Error, ErrorKind},
variables::{Functio, Value, Variable},
};
@ -42,12 +43,17 @@ pub struct ExecEnv {
/// A set of visible variable and function definitions in a single
/// stack frame.
#[derive(Default)]
struct Scope {
/// The mapping from variable names to values.
variables: HashMap<String, Variable>,
// In the future, this will store functio definitions and possibly
// other information.
}
impl Default for Scope {
fn default() -> Self {
Self {
variables: ablescript_consts(),
}
}
}
/// The reason a successful series of statements halted.