From a21f18da6efd55dc9d24faf65868cbe5417827da Mon Sep 17 00:00:00 2001 From: John Doty Date: Thu, 15 Feb 2024 06:29:56 -0800 Subject: [PATCH] [fine] export, dump source map, lookup fix --- fine/src/lib.rs | 11 ++--------- fine/src/parser.rs | 33 +++++++++++++++++++++++++++++++++ fine/src/semantics.rs | 28 +++++++++++++++++++++++----- fine/src/tokens.rs | 9 +++++++-- 4 files changed, 65 insertions(+), 16 deletions(-) diff --git a/fine/src/lib.rs b/fine/src/lib.rs index 51dfaec4..a9ba0872 100644 --- a/fine/src/lib.rs +++ b/fine/src/lib.rs @@ -41,17 +41,10 @@ impl ModuleLoader for StandardModuleLoader { let result = match std::fs::canonicalize(&p) { Ok(p) => match p.into_os_string().into_string() { Ok(s) => s, - Err(_e) => { - eprintln!("ERROR INTO OS STRING: {}", _e.to_string_lossy()); - name.clone() - } + Err(_e) => name.clone(), }, - Err(_e) => { - eprintln!("ERROR CANONICAL {}: {_e}", p.display()); - name.clone() - } + Err(_e) => name.clone(), }; - eprintln!("**** {source} {name} => {result}"); result } diff --git a/fine/src/parser.rs b/fine/src/parser.rs index 313ad0dd..df006d3c 100644 --- a/fine/src/parser.rs +++ b/fine/src/parser.rs @@ -167,6 +167,8 @@ pub enum TreeKind { WildcardPattern, Import, + Export, + ExportList, } pub struct Tree { @@ -576,6 +578,7 @@ fn file(p: &mut CParser) { match p.peek() { TokenKind::Import => import(p), TokenKind::Class => class(p), + TokenKind::Export => export(p), TokenKind::RightBrace => { // An error parsing mismatched braces can leave me at an // un-balanced right brace, which unfortunately will not be @@ -636,6 +639,36 @@ fn import(p: &mut CParser) { p.end(m, TreeKind::Import); } +fn export(p: &mut CParser) { + let m = p.start(); + + p.expect_start(TokenKind::Export); + match p.peek() { + TokenKind::Fun => function(p), + TokenKind::Let => statement_let(p), + TokenKind::Identifier => export_list(p), + TokenKind::Semicolon => (), + _ => p.error("expected something to export"), + } + + p.end(m, TreeKind::Export); +} + +fn export_list(p: &mut CParser) { + let m = p.start(); + + p.expect_start(TokenKind::Identifier); + while p.eat(TokenKind::Comma) { + p.expect( + TokenKind::Identifier, + "expected an identifier to export in this export list", + ); + } + p.expect(TokenKind::Semicolon, "expected a ; to end the export list"); + + p.end(m, TreeKind::ExportList); +} + fn class(p: &mut CParser) { let m = p.start(); diff --git a/fine/src/semantics.rs b/fine/src/semantics.rs index 4ed8009b..d8b21734 100644 --- a/fine/src/semantics.rs +++ b/fine/src/semantics.rs @@ -163,7 +163,7 @@ pub enum Type { Alternate(Box<[Type]>), // A module of some kind. What module? - Module(ImportRecord), + Module(Rc, ImportRecord), } impl Type { @@ -258,7 +258,7 @@ impl fmt::Display for Type { } Ok(()) } - Module(name) => write!(f, "module {}", name.name), + Module(name, _) => write!(f, "module {}", name), } } } @@ -2245,9 +2245,9 @@ impl Semantics { self.internal_compiler_error(None, "import map not initialized"); }; - let name = tok.as_str(&self.source); - match import_map.get(name) { - Some(import) => Some(Type::Module(import.clone())), + let name = string_constant_to_string(tok.as_str(&self.source)); + match import_map.get(&name) { + Some(import) => Some(Type::Module(name.into(), import.clone())), None => { self.report_error_tree(tree, format!("unable to resolve module import {name}")); Some(Type::Error) @@ -2405,6 +2405,21 @@ impl Semantics { } } + { + match self.import_map.get() { + Some(m) => { + eprintln!("Import map:"); + for (k, b) in m.iter() { + eprintln!(" {k} => {} ({})", b.name, b.module_id); + } + eprintln!(); + } + None => { + eprintln!("The import map is not set.\n") + } + } + } + if let Some(tr) = tr { eprintln!("This is about the tree: {:?}", &self.syntax_tree[tr]); eprintln!("The logical parent chain of the tree was:\n"); @@ -2529,6 +2544,9 @@ pub fn check(s: &Semantics) { TreeKind::Import => { let _ = s.type_of(t); } + + TreeKind::Export => {} + TreeKind::ExportList => {} } } } diff --git a/fine/src/tokens.rs b/fine/src/tokens.rs index b4e84e69..e9d1230a 100644 --- a/fine/src/tokens.rs +++ b/fine/src/tokens.rs @@ -42,6 +42,7 @@ pub enum TokenKind { Await, Class, Else, + Export, False, For, From, @@ -294,6 +295,9 @@ impl<'a> Tokens<'a> { if ident == "else" { return TokenKind::Else; } + if ident == "export" { + return TokenKind::Export; + } } 'f' => { if ident == "false" { @@ -639,12 +643,13 @@ mod tests { test_tokens!( more_more_keywords, - "in is match _ as", + "in is match _ as export", (0, In, "in"), (3, Is, "is"), (6, Match, "match"), (12, Underscore, "_"), - (14, As, "as") + (14, As, "as"), + (17, Export, "export") ); test_tokens!(