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 //! Number constants.
pub const TAU: i32 = 6;
pub const PI: i32 = 3; use std::{cell::RefCell, collections::HashMap, rc::Rc};
pub const E: i32 = 3;
pub const M: i32 = 70; // @Kev#6900's weight in kilograms use crate::variables::{Value, Variable};
pub const PHI: i32 = 2;
pub const GOLDEN_RATIO: i32 = 2; /// Initialize a HashMap between the constant names and values
pub const WUA: i32 = 1; /// accessible from within AbleScript.
pub const EULERS_CONSTANT: i32 = 0; pub fn ablescript_consts() -> HashMap<String, Variable> {
pub const GRAVITY: i32 = 10; use Value::*;
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/) let mut map = HashMap::new();
pub const INF: i32 = i32::max_value(); for (name, value) in &[
pub const INTERESSANT: i32 = 114514; // HTGAzureX1212.#5959 interessant number ("TAU", Int(6)), // Circumference / radius
pub const FUNNY: i32 = 69; // HTGAzureX1212.#5959 funny number ("PI", Int(3)), // Deprecated, do not use
pub const NEVERGONNAGIVEYOUUP: &str = "1452251871514141792252515212116"; ("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; pub const ANSWER: i32 = 42;

View file

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