Skip to content

Commit

Permalink
Merge pull request #7 from jwillbold/dev
Browse files Browse the repository at this point in the history
Implemented try & throw and some minors
  • Loading branch information
jwillbold committed Dec 8, 2019
2 parents 479b4d0 + 7741372 commit 5368b88
Show file tree
Hide file tree
Showing 17 changed files with 614 additions and 115 deletions.
86 changes: 85 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ These are the properties that are not reflected by the bytecode as they would be
This compiler currently only supports a subset of JavaScript features. Currently missing are
- Object related notations ({}, new, this, super, class)
- for-of and for-in loops
- try and throw structures
- async and await keywords
- with, and switch keywords
- ~~try and throw structures~~
- ~~break, continue, labels~~
- function expressions and arrow function (Regular functions are allowed)
- function expressions and arrow functions can be realized with:
Expand Down
2 changes: 1 addition & 1 deletion compiler-interface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ assert_cmd = "0.11.1"
resw = "~0.2.2"
ressa = "~0.5.2"
resast = "~0.2.1"
clap = "~2.33.0"
structopt = "0.3"
51 changes: 43 additions & 8 deletions compiler-interface/src/composer.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::errors::{CompositionError, CompositionResult};

use jsyc_compiler::{Bytecode, JSSourceCode, JSAst, DeclDepencies};
use resast::prelude::*;

use crate::errors::{CompositionError, CompositionResult};
use crate::options::{Options, VMExportType};


#[derive(Debug, PartialEq)]
pub struct VM {
vm_template: Vec<resast::ProgramPart>
Expand Down Expand Up @@ -114,23 +116,56 @@ impl VM {
}).collect()
})
}

pub fn append_export_stmt(&mut self, export_types: &VMExportType) {
use resast::*;
self.vm_template.push(match export_types {
VMExportType::NodeJS => {
ProgramPart::Stmt(Stmt::Expr(Expr::Assignment(AssignmentExpr{
operator: AssignmentOperator::Equal,
left: AssignmentLeft::Expr(Box::new(Expr::Member(MemberExpr{
object: Box::new(Expr::Ident("module".into())),
property: Box::new(Expr::Ident("exports".into())),
computed: false
}))),
right: Box::new(Expr::Ident("VM".into()))
})))
},
VMExportType::ES6 => {
ProgramPart::Decl(Decl::Export(Box::new(ModExport::Named(NamedExportDecl::Specifier(
vec![ExportSpecifier::new("VM".into(), None)],
None
)))))
}
});
}


}

pub struct Composer {
pub struct Composer<'a> {
vm: VM,
bytecode: Bytecode
bytecode: Bytecode,
options: &'a Options
}

impl Composer {
pub fn new(vm: VM, bytecode: Bytecode) -> Self {
impl<'a> Composer<'a> {
pub fn new(vm: VM, bytecode: Bytecode, options: &'a Options) -> Self {
Composer {
vm,
bytecode
bytecode,
options
}
}

pub fn compose(self, decls_deps: &DeclDepencies) -> CompositionResult<(VM, Bytecode)> {
Ok((self.vm.inject_decleration_dependencies(decls_deps)?.strip_unneeded()?, self.bytecode))
let mut vm = self.vm.inject_decleration_dependencies(decls_deps)?.strip_unneeded()?;

if let Some(export_type) = &self.options.vm_options.export_type {
vm.append_export_stmt(export_type);
}

Ok((vm, self.bytecode))
}
}

Expand Down
Loading

0 comments on commit 5368b88

Please sign in to comment.