mirror of
https://github.com/voltrevo/ValueScript.git
synced 2026-04-18 03:00:27 -04:00
Add export line to assembly
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
#[derive(Debug, Default)]
|
||||
#[derive(Debug)]
|
||||
pub struct Module {
|
||||
pub export_default: Value,
|
||||
pub export_star: Object,
|
||||
pub definitions: Vec<Definition>,
|
||||
}
|
||||
|
||||
@@ -13,18 +15,32 @@ impl Module {
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Module {
|
||||
fn default() -> Self {
|
||||
Module {
|
||||
export_default: Value::Void,
|
||||
export_star: Object::default(),
|
||||
definitions: Vec::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Module {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
let mut first = true;
|
||||
if self.export_star.properties.len() == 0 {
|
||||
write!(f, "export {} {}", self.export_default, self.export_star)?;
|
||||
} else {
|
||||
write!(f, "export {} * {{\n", self.export_default)?;
|
||||
|
||||
for definition in &self.definitions {
|
||||
if first {
|
||||
first = false;
|
||||
} else {
|
||||
write!(f, "\n\n")?;
|
||||
for (name, value) in &self.export_star.properties {
|
||||
write!(f, " {}: {},\n", name, value)?;
|
||||
}
|
||||
|
||||
write!(f, "{}", definition)?;
|
||||
write!(f, "}}")?;
|
||||
}
|
||||
|
||||
for definition in &self.definitions {
|
||||
write!(f, "\n\n{}", definition)?;
|
||||
}
|
||||
|
||||
return Ok(());
|
||||
|
||||
@@ -13,6 +13,15 @@ struct AssemblyParser<'a> {
|
||||
|
||||
impl<'a> AssemblyParser<'a> {
|
||||
fn module(&mut self) -> Module {
|
||||
self.parse_exact("export");
|
||||
self.parse_whitespace();
|
||||
|
||||
let export_default = self.assemble_value();
|
||||
self.parse_whitespace();
|
||||
|
||||
let export_star = self.assemble_object();
|
||||
self.parse_whitespace();
|
||||
|
||||
let mut definitions = Vec::<Definition>::new();
|
||||
|
||||
loop {
|
||||
@@ -25,7 +34,11 @@ impl<'a> AssemblyParser<'a> {
|
||||
definitions.push(self.assemble_definition());
|
||||
}
|
||||
|
||||
Module { definitions }
|
||||
Module {
|
||||
export_default,
|
||||
export_star,
|
||||
definitions,
|
||||
}
|
||||
}
|
||||
|
||||
fn get_pos_index(&self) -> usize {
|
||||
@@ -103,6 +116,25 @@ impl<'a> AssemblyParser<'a> {
|
||||
return true;
|
||||
}
|
||||
|
||||
fn parse_whitespace(&mut self) {
|
||||
let mut count = 0;
|
||||
|
||||
loop {
|
||||
match self.pos.peek() {
|
||||
Some(' ') => (),
|
||||
Some('\n') => (),
|
||||
_ => break,
|
||||
}
|
||||
|
||||
count += 1;
|
||||
self.pos.next();
|
||||
}
|
||||
|
||||
if count == 0 {
|
||||
panic!("{}", self.render_pos(0, &"Expected whitespace".to_string()));
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_optional_whitespace(&mut self) {
|
||||
loop {
|
||||
match self.pos.peek() {
|
||||
|
||||
@@ -419,6 +419,8 @@ impl ModuleCompiler {
|
||||
// First compile default
|
||||
match default_export_pointer {
|
||||
Some(default_export_pointer) => {
|
||||
self.module.export_default = Value::Pointer(default_export_pointer.clone());
|
||||
|
||||
for module_item in &module.body {
|
||||
match module_item {
|
||||
ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultDecl(edd)) => self
|
||||
|
||||
Reference in New Issue
Block a user