Add index of variants to symbol table

Also new prelude type, just for testing
This commit is contained in:
greg 2018-08-05 13:59:13 -07:00
parent 7548bdbb78
commit 42d0aba21c
3 changed files with 10 additions and 6 deletions

View File

@ -181,7 +181,7 @@ impl<'a> State<'a> {
{
let symbol_table = self.symbol_table_handle.borrow();
match symbol_table.values.get(&name) {
Some(Symbol { spec: SymbolSpec::DataConstructor { type_name, type_args }, name }) => {
Some(Symbol { spec: SymbolSpec::DataConstructor { type_name, type_args, .. }, name }) => {
if args.len() != type_args.len() {
return Err(format!("Data constructor {} requires {} args", name, type_args.len()));
}
@ -292,7 +292,7 @@ impl<'a> State<'a> {
let value = symbol_table.values.get(&name);
Ok(match value {
Some(Symbol { name, spec }) => match spec {
SymbolSpec::DataConstructor { type_name, type_args } => {
SymbolSpec::DataConstructor { type_name, type_args, .. } => {
if type_args.len() == 0 {
Expr::Lit(Lit::Custom(name.clone(), vec![]))
} else {

View File

@ -56,6 +56,7 @@ impl Schala {
pub fn new() -> Schala {
let prelude = r#"
type Option<T> = Some(T) | None
type Color = Red | Green | Blue
"#;
let mut s = Schala::new_blank_env();
s.execute_pipeline(prelude, &EvalOptions::default());

View File

@ -33,6 +33,7 @@ impl fmt::Display for Symbol {
pub enum SymbolSpec {
Func(Vec<TypeName>),
DataConstructor {
index: usize,
type_name: Rc<String>,
type_args: Vec<Rc<String>>,
},
@ -43,7 +44,7 @@ impl fmt::Display for SymbolSpec {
use self::SymbolSpec::*;
match self {
Func(type_names) => write!(f, "Func({:?})", type_names),
DataConstructor { type_name, type_args } => write!(f, "DataConstructor({:?} -> {})", type_args, type_name),
DataConstructor { index, type_name, type_args } => write!(f, "DataConstructor({})({:?} -> {})", index, type_args, type_name),
}
}
}
@ -79,11 +80,12 @@ impl SymbolTable {
Symbol { name: signature.name.clone(), spec }
);
},
TypeDecl { name: TypeSingletonName { name, params}, body: TypeBody(variants), mutable } => {
for var in variants {
TypeDecl { name: TypeSingletonName { name, params}, body: TypeBody(variants), mutable, } => {
for (index, var) in variants.iter().enumerate() {
match var {
Variant::UnitStruct(variant_name) => {
let spec = SymbolSpec::DataConstructor {
index,
type_name: name.clone(),
type_args: vec![],
};
@ -94,7 +96,8 @@ impl SymbolTable {
TypeName::Singleton(TypeSingletonName { name, ..}) => name.clone(),
TypeName::Tuple(_) => unimplemented!(),
}).collect();
let spec = SymbolSpec::DataConstructor {
let spec = SymbolSpec::DataConstructor {
index,
type_name: name.clone(),
type_args
};