Add IntValue type alias for backend integer values

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2020-10-20 13:10:53 -07:00
parent d449632c5b
commit 9169f5970e
4 changed files with 10 additions and 9 deletions

View File

@@ -56,7 +56,7 @@ impl Compile {
}
/// Gets or inserts a static int reference.
pub fn const_int(&mut self, int: i64) -> (ConstHandle, IntRef) {
pub fn const_int(&mut self, int: IntValue) -> (ConstHandle, IntRef) {
self.const_data_mut().const_int(int)
}
@@ -115,14 +115,14 @@ impl Compile {
/// This distinguishes between the different types while building a const pool to avoid duplicates.
#[derive(Debug, Default)]
pub struct ConstData {
ints: HashMap<i64, (ConstHandle, IntRef)>,
ints: HashMap<IntValue, (ConstHandle, IntRef)>,
strs: HashMap<String, (ConstHandle, StrRef)>,
const_pool: ConstPool,
}
impl ConstData {
/// Gets or inserts a static int reference.
pub fn const_int(&mut self, int: i64) -> (ConstHandle, IntRef) {
pub fn const_int(&mut self, int: IntValue) -> (ConstHandle, IntRef) {
if let Some(pair) = self.ints.get(&int) {
pair.clone()
} else {

View File

@@ -4,16 +4,17 @@ use shredder::Scan;
use std::fmt::{Debug, Formatter, self};
pub type IntRef = ObjRef<Int>;
pub type IntValue = i64;
#[derive(Scan)]
pub struct Int {
value: i64,
value: IntValue,
vtable: Vtable,
attrs: Attrs,
}
impl Int {
pub fn new_obj(value: i64) -> ObjRef<Self> {
pub fn new_obj(value: IntValue) -> ObjRef<Self> {
// TODO(cache) : cache int values
self_referring_obj! {
Self {
@@ -33,7 +34,7 @@ impl Int {
}
}
pub fn value(&self) -> i64 {
pub fn value(&self) -> IntValue {
self.value
}
}

View File

@@ -86,7 +86,7 @@ static STR_INT_FUN: Lazy<NativeFunRef> = Lazy::new(|| {
error: "expected Str value".to_string(),
})?;
let int: i64 = str_obj.value.parse()
let int: IntValue = str_obj.value.parse()
.map_err(|_| Error::ValueError { error: "invalid Int value".to_string(), })?;
vm.push(Int::new_obj(int));

View File

@@ -1,4 +1,4 @@
use crate::syn::visit::*;
use crate::{obj::int::IntValue, syn::visit::*};
// TODO : add locations to parsed items
@@ -444,7 +444,7 @@ impl<V: Visit> DefaultAccept<V> for FunExpr {
pub enum Atom {
Ident(String),
Sym(String),
Num(i64),
Num(IntValue),
String(String),
}