Skip to content

Commit

Permalink
Updates to parser (#67)
Browse files Browse the repository at this point in the history
* Change crate from generator and more
  • Loading branch information
kaleidawave authored Oct 18, 2023
1 parent 7d1ff5b commit a1814a5
Show file tree
Hide file tree
Showing 31 changed files with 804 additions and 577 deletions.
25 changes: 21 additions & 4 deletions .github/workflows/performance-and-size.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,28 @@ jobs:
env:
CARGO_PROFILE_RELEASE_DEBUG: true

- name: Run hyperfine
- name: Run parser (and stringer) performance
run: |
curl -O https://gist.githubusercontent.com/kaleidawave/9554eb0ec0a2efc5727a3227fe997c8d/raw/6445ec1b802b52081e6dbb9c3a99e6de3f33dcfa/example.js
hyperfine './target/release/ezno build example.js'
curl https://esm.sh/v128/[email protected]/es2022/react-dom.mjs > react.js
echo "### Hyperfine">> $GITHUB_STEP_SUMMARY
echo "\`\`\`shell">> $GITHUB_STEP_SUMMARY
hyperfine './target/release/ezno ast-explorer --file react.js uglifier' >> $GITHUB_STEP_SUMMARY
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
- name: Run checker performance
run: |
curl -O https://gist.githubusercontent.com/kaleidawave/5dcb9ec03deef1161ebf0c9d6e4b88d8/raw/26c26e908a7c6b79a2e93627f1fefa7ffccbd389/demo.ts > demo.ts
echo "### Output">> $GITHUB_STEP_SUMMARY
echo "\`\`\`shell">> $GITHUB_STEP_SUMMARY
./target/release/ezno check demo.ts >> $GITHUB_STEP_SUMMARY
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
echo "### Hyperfine">> $GITHUB_STEP_SUMMARY
echo "\`\`\`shell">> $GITHUB_STEP_SUMMARY
hyperfine './target/release/ezno check demo.ts' >> $GITHUB_STEP_SUMMARY
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
- name: Print (linux) binary size
run: |
echo "Binary is $(stat -c %s ./target/release/ezno) bytes"
echo "Binary is $(stat -c %s ./target/release/ezno) bytes" >> $GITHUB_STEP_SUMMARY
4 changes: 4 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ jobs:
- 'parser/**'
checker:
- 'checker/**'
src:
- 'src/**'
- name: Check source is valid
run: cargo check --workspace
Expand All @@ -54,9 +56,11 @@ jobs:
run: cargo check --bin ezno

- uses: brndnmtthws/rust-action-cargo-binstall@v1
if: steps.changes.outputs.src == 'true'
with:
packages: [email protected]
- name: Check WASM
if: steps.changes.outputs.src == 'true'
run: |
rustup target add wasm32-unknown-unknown
# Need to build to check that the JS builds
Expand Down
4 changes: 2 additions & 2 deletions checker/src/synthesis/assignments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ pub(crate) fn synthesise_access_to_reference<T: crate::ReadFromFS>(
VariableOrPropertyAccess::PropertyAccess { parent, property, position } => {
let parent_ty = synthesise_expression(&parent, environment, checking_data);
let key_ty = match property {
parser::PropertyReference::Standard(prop) => {
checking_data.types.new_constant_type(Constant::String(prop.clone()))
parser::PropertyReference::Standard { property, is_private: _ } => {
checking_data.types.new_constant_type(Constant::String(property.clone()))
}
parser::PropertyReference::Cursor(_) => todo!(),
};
Expand Down
62 changes: 31 additions & 31 deletions checker/src/synthesis/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,37 +60,37 @@ pub(super) fn synthesise_class_declaration<
);
};

let extends = if let Some(ref extends) = class.extends {
fn build_extends_type<'a, T: crate::ReadFromFS>(
mut extends: impl Iterator<Item = &'a TypeAnnotation>,
environment: &mut Environment,
checking_data: &mut CheckingData<T, parser::Module>,
on: TypeId,
) -> TypeId {
let mut ty = synthesise_type_annotation(
extends.next().unwrap(),
environment,
checking_data,
);

for reference in extends {
let rhs = synthesise_type_annotation(reference, environment, checking_data);
// TODOsynthesise_type_annotation
ty = checking_data.types.register_type(Type::And(ty, rhs));
}

environment.bases.connect_extends(on, ty);

ty
}

let result =
build_extends_type(iter::once(extends), environment, checking_data, class_type);

Some(result)
} else {
None
};
// let extends = if let Some(ref extends) = class.extends {
// fn build_extends_type<'a, T: crate::ReadFromFS>(
// mut extends: impl Iterator<Item = &'a TypeAnnotation>,
// environment: &mut Environment,
// checking_data: &mut CheckingData<T, parser::Module>,
// on: TypeId,
// ) -> TypeId {
// let mut ty = synthesise_type_annotation(
// extends.next().unwrap(),
// environment,
// checking_data,
// );

// for reference in extends {
// let rhs = synthesise_type_annotation(reference, environment, checking_data);
// // TODOsynthesise_type_annotation
// ty = checking_data.types.register_type(Type::And(ty, rhs));
// }

// environment.bases.connect_extends(on, ty);

// ty
// }

// let result =
// build_extends_type(iter::once(extends), environment, checking_data, class_type);

// Some(result)
// } else {
// None
// };

let mut class_constructor = None;
let mut properties = Vec::<(TypeId, Expression)>::new();
Expand Down
50 changes: 18 additions & 32 deletions checker/src/synthesis/definitions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use parser::ASTNode;

use crate::{context::RootContext, synthesis::functions::type_function_reference};
use crate::{context::RootContext, synthesis::functions::type_function_reference, TypeId};

const DEFINITION_VAR_IS_CONSTANT: bool = true;

Expand Down Expand Up @@ -102,41 +102,27 @@ pub(super) fn type_definition_file<T: crate::ReadFromFS>(
);
}
TypeDefinitionModuleDeclaration::Variable(DeclareVariableDeclaration {
name,
type_restriction,
decorators,
keyword,
declarations,
position,
}) => {
// TODO tidy up
let variable_ty =
synthesise_type_annotation(&type_restriction, root, checking_data);
for declaration in declarations.iter() {
let constraint = declaration.type_annotation.as_ref().map(|annotation| {
synthesise_type_annotation(annotation, root, checking_data)
});

// // TODO not sure...
// if let Some(frozen) = root.is_frozen(variable_ty) {
// root.frozen.insert(var_type, frozen);
// }

let position = position.clone().with_source(source);
let declare_variable = root.declare_variable(
&name,
position.clone(),
variable_ty,
&mut checking_data.types,
);

checking_data
.type_mappings
.variables_to_constraints
.0
.insert(crate::VariableId(source, position.start), variable_ty);
// TODO warning here
let behavior = crate::context::VariableRegisterBehavior::Declare {
base: constraint.unwrap_or(TypeId::ANY_TYPE),
};

if let Err(error) = declare_variable {
checking_data.diagnostics_container.add_error(
TypeCheckError::CannotRedeclareVariable {
name: error.name.to_owned(),
position,
},
)
crate::synthesis::variables::register_variable(
declaration.name.get_ast_ref(),
root,
checking_data,
behavior,
constraint,
);
}
}
TypeDefinitionModuleDeclaration::Interface(interface) => {
Expand Down
15 changes: 8 additions & 7 deletions checker/src/synthesis/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,6 @@ pub(super) fn synthesise_expression<T: crate::ReadFromFS>(
| BinaryOperator::NullCoalescing => {
unreachable!()
}
BinaryOperator::InstanceOf => todo!(),
BinaryOperator::In => todo!(),
BinaryOperator::Divides => todo!(),
BinaryOperator::Pipe => todo!(),
BinaryOperator::Compose => todo!(),
Expand Down Expand Up @@ -382,11 +380,12 @@ pub(super) fn synthesise_expression<T: crate::ReadFromFS>(
}
Expression::PropertyAccess { parent, position, property, .. } => {
let on = synthesise_expression(parent, environment, checking_data);
let property = if let parser::PropertyReference::Standard(name) = property {
checking_data.types.new_constant_type(Constant::String(name.clone()))
} else {
todo!()
};
let property =
if let parser::PropertyReference::Standard { property, is_private: _ } = property {
checking_data.types.new_constant_type(Constant::String(property.clone()))
} else {
todo!()
};
let get_property =
environment.get_property(on, property, &mut checking_data.types, None);

Expand Down Expand Up @@ -642,6 +641,8 @@ pub(super) fn synthesise_expression<T: crate::ReadFromFS>(

return value;
}
SpecialOperators::InExpression { .. } => todo!(),
SpecialOperators::InstanceOfExpression { .. } => todo!(),
},
Expression::DynamicImport { path, position } => todo!(),
Expression::IsExpression(is_expr) => {
Expand Down
26 changes: 15 additions & 11 deletions checker/src/synthesis/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,16 @@ impl FunctionBasedItem for parser::functions::bases::ArrowFunctionBase {
}
}

impl<'a> From<&'a parser::GetSetGeneratorOrNone> for crate::GetterSetterGeneratorOrNone {
fn from(value: &'a parser::GetSetGeneratorOrNone) -> Self {
impl<'a> From<&'a Option<parser::MethodHeader>> for GetterSetterGeneratorOrNone {
fn from(value: &'a Option<parser::MethodHeader>) -> Self {
match value {
parser::GetSetGeneratorOrNone::Get(_) => GetterSetterGeneratorOrNone::Getter,
parser::GetSetGeneratorOrNone::Set(_) => GetterSetterGeneratorOrNone::Setter,
parser::GetSetGeneratorOrNone::Generator(_)
| parser::GetSetGeneratorOrNone::GeneratorStar(_) => GetterSetterGeneratorOrNone::Generator,
parser::GetSetGeneratorOrNone::None => GetterSetterGeneratorOrNone::None,
Some(parser::MethodHeader::Get(_)) => GetterSetterGeneratorOrNone::Getter,
Some(parser::MethodHeader::Set(_)) => GetterSetterGeneratorOrNone::Setter,
Some(
parser::MethodHeader::Generator(a, _) | parser::MethodHeader::GeneratorStar(a, _),
) => GetterSetterGeneratorOrNone::Generator,
// TODO temp
Some(parser::MethodHeader::Async(_)) | None => GetterSetterGeneratorOrNone::None,
}
}
}
Expand All @@ -94,23 +96,25 @@ impl FunctionBasedItem for parser::functions::bases::ObjectLiteralMethodBase {
type ObjectTypeId = Option<TypeId>;

fn get_set_generator_or_none(func: &FunctionBase<Self>) -> GetterSetterGeneratorOrNone {
From::from(&func.header.1)
From::from(&func.header)
}

fn is_async(func: &FunctionBase<Self>) -> bool {
func.header.0.is_some()
// TODO temp
false
}
}

impl FunctionBasedItem for parser::functions::bases::ClassFunctionBase {
type ObjectTypeId = Option<TypeId>;

fn get_set_generator_or_none(func: &FunctionBase<Self>) -> GetterSetterGeneratorOrNone {
From::from(&func.header.1)
From::from(&func.header)
}

fn is_async(func: &FunctionBase<Self>) -> bool {
func.header.0.is_some()
// TODO temp
false
}
}

Expand Down
48 changes: 19 additions & 29 deletions checker/src/synthesis/hoisting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,39 +216,29 @@ pub(crate) fn hoist_statements<T: crate::ReadFromFS>(
}
parser::Declaration::TypeAlias(_) => {}
parser::Declaration::DeclareVariable(DeclareVariableDeclaration {
name,
type_restriction,
decorators,
keyword: _,
declarations,
position,
}) => {
// TODO tidy up
let variable_ty =
synthesise_type_annotation(&type_restriction, environment, checking_data);

// // TODO not sure...
// if let Some(frozen) = environment.is_frozen(variable_ty) {
// environment.frozen.insert(var_type, frozen);
// }

let declare_variable = environment.declare_variable(
&name,
position.clone().with_source(environment.get_source()),
variable_ty,
&mut checking_data.types,
);
for declaration in declarations.iter() {
let constraint = get_annotation_from_declaration(
declaration,
environment,
checking_data,
);

checking_data.type_mappings.variables_to_constraints.0.insert(
crate::VariableId(environment.get_source(), position.start),
variable_ty,
);
// TODO warning here
let behavior = crate::context::VariableRegisterBehavior::Declare {
base: constraint.unwrap_or(TypeId::ANY_TYPE),
};

if let Err(error) = declare_variable {
checking_data.diagnostics_container.add_error(
crate::diagnostics::TypeCheckError::CannotRedeclareVariable {
name: error.name.to_owned(),
position: position.clone().with_source(environment.get_source()),
},
)
register_variable(
declaration.name.get_ast_ref(),
environment,
checking_data,
behavior,
constraint,
);
}
}
parser::Declaration::DeclareInterface(_) => {}
Expand Down
1 change: 1 addition & 0 deletions checker/src/synthesis/interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ pub(super) fn synthesise_signatures<
match &signature.on {
InterfaceMember::Method {
name,
kind,
type_parameters,
parameters,
return_type,
Expand Down
13 changes: 9 additions & 4 deletions checker/src/synthesis/variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use parser::{

use super::{expressions::synthesise_expression, type_annotations::synthesise_type_annotation};
use crate::{
context::{Context, ContextType},
diagnostics::{TypeCheckError, TypeStringRepresentation},
synthesis::property_key_as_type,
types::Constant,
Expand All @@ -17,16 +18,20 @@ use crate::{
/// For eagerly registering variables, before the statement and its RHS is actually evaluate
///
/// TODO shouldn't return type, for performs...
pub(crate) fn register_variable<T: crate::ReadFromFS, U: parser::VariableFieldKind>(
pub(crate) fn register_variable<
T: crate::ReadFromFS,
U: parser::VariableFieldKind,
V: ContextType,
>(
name: &parser::VariableField<U>,
environment: &mut Environment,
environment: &mut Context<V>,
checking_data: &mut CheckingData<'_, T, parser::Module>,
behavior: crate::context::VariableRegisterBehavior,
constraint: Option<TypeId>,
) -> TypeId {
fn register_variable_identifier<T: crate::ReadFromFS>(
fn register_variable_identifier<T: crate::ReadFromFS, V: ContextType>(
name: &VariableIdentifier,
environment: &mut Environment,
environment: &mut Context<V>,
checking_data: &mut CheckingData<T, parser::Module>,
behavior: crate::context::VariableRegisterBehavior,
constraint: Option<TypeId>,
Expand Down
Loading

0 comments on commit a1814a5

Please sign in to comment.