-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] Add first-class method calls; put in table
- Loading branch information
Showing
12 changed files
with
125 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use crate::{shared::Type, typ}; | ||
use lazy_static::lazy_static; | ||
use std::collections::HashMap; | ||
|
||
macro_rules! entry { | ||
($name:ident, $($args:tt -> $ret:ident),+) => { | ||
(stringify!($name), vec![$(typ!(fun $args -> $ret)),+]) | ||
}; | ||
} | ||
|
||
fn methods_table() -> HashMap<&'static str, Vec<Type>> { | ||
[ | ||
entry!(slice, (string, int, int) -> string, (array, int, int) -> array), | ||
entry!(at, (string, int) -> any, (string, int) -> string), | ||
entry!(concat, (array, array) -> array, (string, string) -> string), | ||
// Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array | ||
// Array.prototype[@@unscopables] // ?? | ||
// length is actually special and not really part of the prototype but | ||
// for the purpose of type inference it should be here | ||
//"length", | ||
// Array.prototype[@@iterator]() // ?? | ||
// get Array[@@species] // ?? | ||
//"copyWithin", | ||
//"entries", | ||
//"every", | ||
//"fill", | ||
//"filter", | ||
//"find", | ||
//"findIndex", | ||
//"flat", | ||
//"flatMap", | ||
//"forEach", | ||
//"from", // Array.from, not prototype | ||
//"includes", | ||
//"indexOf", | ||
//"isArray", // Array.isArray | ||
//"join", | ||
//"keys", | ||
//"lastIndexOf", | ||
//"map", | ||
//"of", // Array.of | ||
//"pop", | ||
//"push", | ||
//"reduce", | ||
//"reduceRight", | ||
//"reverse", | ||
//"shift", | ||
//"slice", | ||
//"some", | ||
//"sort", | ||
//"splice", | ||
//"toLocaleString", | ||
//"toSource", | ||
//"toString", | ||
//"unshift", | ||
//"values", | ||
] | ||
.iter() | ||
.cloned() | ||
.collect() | ||
} | ||
|
||
lazy_static! { | ||
pub static ref METHODS_TABLE: HashMap<&'static str, Vec<Type>> = methods_table(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use super::syntax::*; | ||
use super::*; | ||
|
||
struct ResugarMethodCall; | ||
|
||
impl Visitor for ResugarMethodCall { | ||
fn exit_expr(&mut self, expr: &mut Expr, _: &Loc) { | ||
match expr { | ||
Expr::Call(f, args, _) => match &mut **f { | ||
Expr::Dot(obj, name, s) => { | ||
let id = if let Expr::Id(obj_name, _) = &**obj { | ||
obj_name.clone() | ||
} else { | ||
panic!("Desugar should have named method objects") | ||
}; | ||
let name = if let Id::Named(field) = name { | ||
std::mem::replace(field, Default::default()) | ||
} else { | ||
panic!("Dot should't access special ids") | ||
}; | ||
let args = std::mem::replace(args, vec![]); | ||
*expr = Expr::MethodCall(id, name, args, s.clone()); | ||
} | ||
_ => (), | ||
}, | ||
_ => (), | ||
} | ||
} | ||
} | ||
|
||
pub fn resugar_method_call(program: &mut Stmt) { | ||
let mut v = ResugarMethodCall; | ||
program.walk(&mut v); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters