From 3d9f847862f7c7cb5fccf4b336baaabea5422b64 Mon Sep 17 00:00:00 2001 From: Alex Bethel Date: Tue, 13 Jul 2021 14:22:06 -0500 Subject: [PATCH] Make consts accessible from AbleScript code --- src/consts.rs | 62 +++++++++++++++++++++++++++++++++++------------- src/interpret.rs | 14 +++++++---- 2 files changed, 55 insertions(+), 21 deletions(-) diff --git a/src/consts.rs b/src/consts.rs index 3a206df..8eaeb51 100644 --- a/src/consts.rs +++ b/src/consts.rs @@ -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 { + 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; diff --git a/src/interpret.rs b/src/interpret.rs index 5f6561a..8290fa3 100644 --- a/src/interpret.rs +++ b/src/interpret.rs @@ -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, - // 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.