constants and static #102
This commit is contained in:
@@ -167,6 +167,26 @@ impl GenMist for TopLevelKind {
|
||||
fn gen_mist(&self, ctx: &mut Context, cg: &mut MistCodegen) {
|
||||
match self {
|
||||
Self::ModAttribute => {}
|
||||
Self::StaticDecl(decl) => {
|
||||
cg.add("static ");
|
||||
decl.decl.gen_mist(ctx, cg);
|
||||
|
||||
if let Some(init) = &decl.init {
|
||||
cg.add(" = ");
|
||||
init.gen_mist(ctx, cg);
|
||||
}
|
||||
cg.add(";");
|
||||
}
|
||||
Self::ConstDecl(decl) => {
|
||||
cg.add("const ");
|
||||
decl.decl.gen_mist(ctx, cg);
|
||||
|
||||
if let Some(init) = &decl.init {
|
||||
cg.add(" = ");
|
||||
init.gen_mist(ctx, cg);
|
||||
}
|
||||
cg.add(";");
|
||||
}
|
||||
Self::Import(vis, path) => {
|
||||
cg.addln(&format!("{}use {};", vis.get_mist(), path.get_mist()))
|
||||
}
|
||||
|
||||
@@ -191,6 +191,28 @@ impl GenRust for TopLevelKind {
|
||||
fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) {
|
||||
match self {
|
||||
Self::ModAttribute => {}
|
||||
Self::ConstDecl(decl) => {
|
||||
cg.add("const ");
|
||||
decl.decl.gen_rust(ctx, cg);
|
||||
|
||||
if let Some(init) = &decl.init {
|
||||
cg.add(" = ");
|
||||
init.gen_rust(ctx, cg);
|
||||
}
|
||||
|
||||
cg.add(";");
|
||||
}
|
||||
Self::StaticDecl(decl) => {
|
||||
cg.add("static ");
|
||||
decl.decl.gen_rust(ctx, cg);
|
||||
|
||||
if let Some(init) = &decl.init {
|
||||
cg.add(" = ");
|
||||
init.gen_rust(ctx, cg);
|
||||
}
|
||||
|
||||
cg.add(";");
|
||||
}
|
||||
Self::Import(vis, path) => {
|
||||
cg.addln(&format!("{}use {};", vis.get_rust(), path.get_rust()))
|
||||
}
|
||||
|
||||
@@ -30,6 +30,8 @@ pub enum TopLevelKind {
|
||||
Import(Visibility, Path),
|
||||
DeclareModule(Visibility, Identifier),
|
||||
ImplDecl(ImplDecl),
|
||||
StaticDecl(VarDeclStmt),
|
||||
ConstDecl(VarDeclStmt),
|
||||
TraitDecl {
|
||||
visibility: Visibility,
|
||||
name: Identifier,
|
||||
|
||||
@@ -34,6 +34,7 @@ keyword = {
|
||||
| "loop"
|
||||
| "unsafe"
|
||||
| "override"
|
||||
| "const"
|
||||
}
|
||||
|
||||
identifier = @{
|
||||
@@ -362,13 +363,19 @@ function_decl = {
|
||||
visibility? ~ ("void" | type_expr) ~ identifier ~ generics_decl? ~ "(" ~ method_args ~ ")" ~ override_kw? ~ (block | semicolon)
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
// CONSTANTS / STATIC
|
||||
// ======================================================
|
||||
const_decl = { "const" ~ var_decl_statement ~ ";" }
|
||||
static_decl = { "static" ~ var_decl_statement ~ ";" }
|
||||
|
||||
// ======================================================
|
||||
// TOP LEVEL
|
||||
// ======================================================
|
||||
|
||||
top_level = {
|
||||
mod_attribute
|
||||
| (attributes ~ (import | impl_for_decl | impl_decl | trait_decl | struct_decl | enum_decl | class_decl | declare_module | function_decl))
|
||||
| (attributes ~ (import | impl_for_decl | impl_decl | trait_decl | struct_decl | enum_decl | class_decl | declare_module | function_decl | const_decl | static_decl))
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
|
||||
@@ -122,6 +122,9 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevelKind {
|
||||
items: collect_recovered(&mut inner)?,
|
||||
}),
|
||||
|
||||
Rule::const_decl => Ok(TopLevelKind::ConstDecl(inner.next().unwrap().try_into()?)),
|
||||
Rule::static_decl => Ok(TopLevelKind::StaticDecl(inner.next().unwrap().try_into()?)),
|
||||
|
||||
_ => AstError::bug_unimplemented(pair),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user