From 26fa4a29eca2f5e2c6d81fe9f756cc5d879954d0 Mon Sep 17 00:00:00 2001 From: greg Date: Wed, 16 Oct 2019 20:22:40 -0700 Subject: [PATCH] Put type names into symbol table --- schala-lang/language/src/eval.rs | 1 + schala-lang/language/src/symbol_table.rs | 13 ++++++++++++- schala-lang/language/src/symbol_table/test.rs | 3 ++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/schala-lang/language/src/eval.rs b/schala-lang/language/src/eval.rs index 0f86731..8b1f1d8 100644 --- a/schala-lang/language/src/eval.rs +++ b/schala-lang/language/src/eval.rs @@ -489,6 +489,7 @@ impl<'a> State<'a> { Some(Binding { val, .. }) => val.clone(), None => return Err(format!("Symbol {} exists in symbol table but not in evaluator table", name)) } + SymbolSpec::Type { name } => return Err(format!("Symbol {} not in scope", name)), }, //TODO ideally this should be returning a runtime error if this is ever None, but it's not //handling all bindings correctly yet diff --git a/schala-lang/language/src/symbol_table.rs b/schala-lang/language/src/symbol_table.rs index cae4c9b..a88f072 100644 --- a/schala-lang/language/src/symbol_table.rs +++ b/schala-lang/language/src/symbol_table.rs @@ -145,7 +145,10 @@ pub enum SymbolSpec { members: HashMap, TypeName>, type_name: TypeName, }, - Binding + Binding, + Type { + name: TypeName + }, } impl fmt::Display for SymbolSpec { @@ -156,6 +159,7 @@ impl fmt::Display for SymbolSpec { DataConstructor { index, type_name, type_args } => write!(f, "DataConstructor(idx: {})({:?} -> {})", index, type_args, type_name), RecordConstructor { type_name, index, ..} => write!(f, "RecordConstructor(idx: {})( -> {})", index, type_name), Binding => write!(f, "Binding"), + Type { name } => write!(f, "Type <{}>", name), } } } @@ -246,6 +250,13 @@ impl SymbolTable { use crate::ast::{TypeIdentifier, Variant}; let TypeBody(variants) = body; let ref type_name = type_name.name; + + + let type_spec = SymbolSpec::Type { + name: type_name.clone(), + }; + self.add_new_symbol(type_name, &scope_name_stack, type_spec); + scope_name_stack.push(ScopeSegment{ name: type_name.clone(), kind: ScopeSegmentKind::Type, diff --git a/schala-lang/language/src/symbol_table/test.rs b/schala-lang/language/src/symbol_table/test.rs index 31d3583..999218b 100644 --- a/schala-lang/language/src/symbol_table/test.rs +++ b/schala-lang/language/src/symbol_table/test.rs @@ -21,7 +21,8 @@ macro_rules! values_in_table { #[test] fn basic_symbol_table() { - values_in_table! { "let a = 10; fn b() { 20 }", &fqsn!("b"; tr) } + values_in_table! { "let a = 10; fn b() { 20 }", &fqsn!("b"; tr) }; + values_in_table! { "type Option = Some(T) | None", &fqsn!("Option"; tr) }; } #[test]