Skip to content

Commit

Permalink
Use IndexMap for constants and signature
Browse files Browse the repository at this point in the history
  • Loading branch information
phorward committed Feb 18, 2023
1 parent 42f83cd commit 53b86f1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 26 deletions.
23 changes: 7 additions & 16 deletions src/compiler/ast.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Compiler's internal Abstract Syntax Tree traversal
use std::collections::HashSet;
use indexmap::IndexMap;
use tokay_macros::tokay_function;
extern crate self as tokay;
use super::*;
Expand Down Expand Up @@ -162,13 +162,8 @@ fn traverse_node_value(compiler: &mut Compiler, node: &Dict) -> ImlValue {
"value_parselet" => {
compiler.parselet_push();

// Generic signature
let mut gen: Vec<(String, Option<ImlValue>)> = Vec::new();
let mut gen_names = HashSet::new();

// Function signature
let mut sig: Vec<(String, Option<ImlValue>)> = Vec::new();
let mut sig_names = HashSet::new();
let mut gen: IndexMap<String, Option<ImlValue>> = IndexMap::new();
let mut sig: IndexMap<String, Option<ImlValue>> = IndexMap::new();

// Traverse the AST
let mut sigs = List::from(node["children"].clone());
Expand All @@ -187,15 +182,13 @@ fn traverse_node_value(compiler: &mut Compiler, node: &Dict) -> ImlValue {
match emit {
"gen" => {
// check if identifier was not provided twice
if gen_names.contains(&ident) {
if gen.contains_key(&ident) {
compiler.errors.push(Error::new(
traverse_node_offset(node),
format!("Generic '{}' already given in signature before", ident),
));

continue;
} else {
gen_names.insert(ident.clone());
}

compiler.set_constant(&ident, ImlValue::Generic(ident.to_string()));
Expand All @@ -213,7 +206,7 @@ fn traverse_node_value(compiler: &mut Compiler, node: &Dict) -> ImlValue {
None
};

gen.push((ident.to_string(), default));
gen.insert(ident.to_string(), default);
//println!("{} {} {:?}", emit.to_string(), ident, default);
}
"arg" => {
Expand Down Expand Up @@ -241,15 +234,13 @@ fn traverse_node_value(compiler: &mut Compiler, node: &Dict) -> ImlValue {
}

// check if identifier was not provided twice
if sig_names.contains(&ident) {
if sig.contains_key(&ident) {
compiler.errors.push(Error::new(
traverse_node_offset(node),
format!("Argument '{}' already given in signature before", ident),
));

continue;
} else {
sig_names.insert(ident.clone());
}

compiler.new_local(&ident);
Expand All @@ -267,7 +258,7 @@ fn traverse_node_value(compiler: &mut Compiler, node: &Dict) -> ImlValue {
None
};

sig.push((ident.to_string(), default));
sig.insert(ident.to_string(), default);
//println!("{} {} {:?}", emit.to_string(), ident, default);
}
_ => unreachable!(),
Expand Down
9 changes: 5 additions & 4 deletions src/compiler/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::error::Error;
use crate::reader::*;
use crate::value::{RefValue, Token};
use crate::vm::*;
use indexmap::IndexMap;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
Expand Down Expand Up @@ -209,8 +210,8 @@ impl Compiler {
offset: Option<Offset>,
name: Option<String>,
severity: Option<u8>,
gen: Option<Vec<(String, Option<ImlValue>)>>,
sig: Option<Vec<(String, Option<ImlValue>)>>,
gen: Option<IndexMap<String, Option<ImlValue>>>,
sig: Option<IndexMap<String, Option<ImlValue>>>,
body: ImlOp,
) -> ImlValue {
assert!(self.scopes.len() > 0 && matches!(self.scopes[0], Scope::Parselet { .. }));
Expand Down Expand Up @@ -240,8 +241,8 @@ impl Compiler {
}
}

let signature = sig.unwrap_or(Vec::new());
let constants = gen.unwrap_or(Vec::new());
let constants = gen.unwrap_or(IndexMap::new());
let signature = sig.unwrap_or(IndexMap::new());

assert!(
signature.len() <= variables.len(),
Expand Down
13 changes: 7 additions & 6 deletions src/compiler/iml/parselet.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
//! Intermediate representation of a parselet
use super::*;
use crate::reader::Offset;
use indexmap::IndexMap;
use std::collections::{HashMap, HashSet};

#[derive(Debug)]
/// Intermediate parselet
pub(in crate::compiler) struct ImlParselet {
pub offset: Option<Offset>, // Offset of definition
pub consuming: bool, // Flag if parselet is consuming
pub severity: u8, // Capture push severity
pub name: Option<String>, // Parselet's name from source (for debugging)
pub constants: Vec<(String, Option<ImlValue>)>, // Constant signature with default constants; generic parselet when set.
pub signature: Vec<(String, Option<ImlValue>)>, // Argument signature with default arguments
pub offset: Option<Offset>, // Offset of definition
pub consuming: bool, // Flag if parselet is consuming
pub severity: u8, // Capture push severity
pub name: Option<String>, // Parselet's name from source (for debugging)
pub constants: IndexMap<String, Option<ImlValue>>, // Constant signature with default constants; generic parselet when set.
pub signature: IndexMap<String, Option<ImlValue>>, // Argument signature with default arguments
pub locals: usize, // Total number of local variables present (including arguments)
pub begin: ImlOp, // Begin-operations
pub end: ImlOp, // End-operations
Expand Down

0 comments on commit 53b86f1

Please sign in to comment.