Importing

This commit is contained in:
2026-05-04 19:29:10 +02:00
parent 544074c26e
commit c717a57f99
4 changed files with 12 additions and 12 deletions
+1 -1
View File
@@ -47,7 +47,7 @@ pub enum BinaryOp {
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub enum TopLevel { pub enum TopLevel {
Import(String), Include(StaticPath),
StructDecl { StructDecl {
export: bool, export: bool,
name: String, name: String,
+5 -4
View File
@@ -12,15 +12,16 @@ string_lit = { "\"" ~ inner_str ~ "\"" }
inner_str = @{ (!"\"" ~ ANY)* } inner_str = @{ (!"\"" ~ ANY)* }
static_path = { identifier ~ ("::" ~ identifier)* } static_path = { identifier ~ ("::" ~ identifier)* }
export = { "public" } export = { "public" }
mutable = { "mut" } mutable = { "mut" }
var_decl = { type_expr ~ mutable? ~ identifier } sharp = _{ "#" }
var_decl = { type_expr ~ mutable? ~ identifier }
// ================= TOP LEVEL ================= // ================= TOP LEVEL =================
top_level = _{ import | function_decl | struct_decl } top_level = _{ import | function_decl | struct_decl }
import = { "import" ~ string_lit ~ ";" } import = { sharp ~ "include" ~ "<" ~ static_path ~ ">" }
field_list = { field ~ ("," ~ field)* ~ ","? } field_list = { field ~ ("," ~ field)* ~ ","? }
field = { export? ~ type_expr ~ identifier } field = { export? ~ type_expr ~ identifier }
+4 -4
View File
@@ -123,10 +123,10 @@ impl TryFrom<pest::iterators::Pair<'_, Rule>> for TopLevel {
type Error = (); type Error = ();
fn try_from(pair: pest::iterators::Pair<Rule>) -> Result<Self, ()> { fn try_from(pair: pest::iterators::Pair<Rule>) -> Result<Self, ()> {
match pair.as_rule() { match pair.as_rule() {
Rule::import => { Rule::import => Ok(TopLevel::Include(StaticPath::from(
let path = pair.into_inner().next().unwrap().as_str().to_string(); pair.into_inner().next().unwrap(),
Ok(TopLevel::Import(path)) ))),
}
Rule::function_decl => { Rule::function_decl => {
let mut inner = pair.into_inner(); let mut inner = pair.into_inner();
+2 -3
View File
@@ -195,9 +195,8 @@ impl ToRust for Block {
impl ToRust for TopLevel { impl ToRust for TopLevel {
fn to_rust(&self, cg: &mut RustCodegen) { fn to_rust(&self, cg: &mut RustCodegen) {
match self { match self {
TopLevel::Import(path) => { TopLevel::Include(path) => {
let path = path.replace('"', ""); cg.addln(&format!("use {};", path.get_rust()));
cg.addln(&format!("use {};", path));
} }
TopLevel::StructDecl { TopLevel::StructDecl {