WIP on enum generation and solving generated errors.

This commit is contained in:
Jesse Brault 2025-09-14 21:06:58 -05:00
parent 434df5642a
commit 44f6ab10af
20 changed files with 146 additions and 83 deletions

View File

@ -1,4 +1,10 @@
use crate::spec::{BooleanBuild, BuildSpec, ChildSpec, EnumBuildSpec, EnumRule, LeafEnumBuildSpec, LeafEnumRule, LeafEnumRuleBuild, LeafEnumRuleBuildChild, LeafStructBuildSpec, LeafStructChild, LeafStructChildType, SingleBooleanChildToBuild, SingleChild, SingleChildToBuild, SingleLiteralChildToBuild, SingleTypeChildToBuild, SkipChild, StructBuildSpec, VecChild, VecChildToBuild, VecTypeChildToBuild};
use crate::spec::{
BooleanBuild, BuildSpec, ChildSpec, EnumBuildSpec, EnumRule, LeafEnumBuildSpec, LeafEnumRule,
LeafEnumRuleBuild, LeafEnumRuleBuildChild, LeafStructBuildSpec, LeafStructChild,
LeafStructChildType, SingleBooleanChildToBuild, SingleChild, SingleChildToBuild,
SingleLiteralChildToBuild, SingleTypeChildToBuild, SkipChild, StructBuildSpec, VecChild,
VecChildToBuild, VecTypeChildToBuild,
};
use crate::util::make_build_fn_name;
use convert_case::{Case, Casing};
use yaml_rust2::{Yaml, YamlLoader};
@ -218,15 +224,13 @@ fn get_enum_rules(rule_specs: &Yaml) -> Vec<EnumRule> {
}
fn get_leaf_struct_child_specs(children: &Yaml) -> Vec<LeafStructChild> {
children.as_vec()
children
.as_vec()
.unwrap()
.iter()
.map(|child_spec| {
let (name, hash) = unwrap_single_member_hash(child_spec);
LeafStructChild::new(
&name,
LeafStructChildType::String
)
LeafStructChild::new(&name, LeafStructChildType::String)
})
.collect::<Vec<_>>()
}

View File

@ -9,13 +9,13 @@ mod util;
use crate::enum_build_fn::make_enum_build_fn;
use crate::leaf_enum_build_fn::make_leaf_enum_build_fn;
use crate::leaf_struct_build_fn::make_leaf_struct_build_fn;
use crate::struct_build_fn::make_struct_build_fn;
use crate::type_gen::make_type;
use proc_macro2::TokenStream;
use quote::quote;
use spec::BuildSpec;
use syn::File;
use crate::leaf_struct_build_fn::make_leaf_struct_build_fn;
fn debug_built_spec(build_spec: &BuildSpec, token_stream: &TokenStream) {
println!("*** BuildSpec ***");

View File

@ -518,7 +518,7 @@ impl LeafStructChild {
pub fn new(name: &str, r#type: LeafStructChildType) -> Self {
Self {
name: name.to_string(),
r#type
r#type,
}
}

View File

@ -2,10 +2,9 @@ use crate::spec::{
BooleanBuild, ChildSpec, SingleBooleanChildToBuild, SingleChildToBuild,
SingleLiteralChildToBuild, SingleTypeChildToBuild, StructBuildSpec, VecChild, VecChildToBuild,
};
use convert_case::{Case, Casing};
use crate::util::make_build_pair;
use proc_macro2::{Ident, TokenStream};
use quote::{format_ident, quote};
use crate::util::make_build_pair;
fn make_vec_child_holder(vec_child: &VecChild) -> TokenStream {
let (child_ident, child_type_ident) = match vec_child.build() {

View File

@ -102,15 +102,35 @@ fn handle_single_type_child(
#child_ident: Box<#child_type_ident>
})
}
accessors.push(quote! {
pub fn #child_ident(&self) -> &#child_type_ident {
self.#child_ident.as_ref()
}
if single_type_child.optional() {
accessors.push(quote! {
pub fn #child_ident(&self) -> Option<&#child_type_ident> {
if let Some(#child_ident) = &self.#child_ident {
Some(#child_ident.as_ref())
} else {
None
}
}
pub fn #child_ident_mut(&mut self) -> &mut #child_type_ident {
self.#child_ident.as_mut()
}
});
pub fn #child_ident_mut(&mut self) -> Option<&mut #child_type_ident> {
if let Some(#child_ident) = &mut self.#child_ident {
Some(#child_ident.as_mut())
} else {
None
}
}
})
} else {
accessors.push(quote! {
pub fn #child_ident(&self) -> &#child_type_ident {
self.#child_ident.as_ref()
}
pub fn #child_ident_mut(&mut self) -> &mut #child_type_ident {
self.#child_ident.as_mut()
}
});
}
}
fn handle_single_boolean_child(

View File

@ -5,6 +5,8 @@ pub mod node {
pub mod build {
//noinspection RsUnusedImport
use crate::parser::Rule;
//noinspection RsUnusedImport
use crate::ast::node::*;
include!(concat!(env!("OUT_DIR"), "/src/ast/build.rs"));
}

View File

@ -4,14 +4,12 @@ pub struct DmModule {
pub enum NamespaceVisibility {
Public,
Partial {
visible_to_fqns: Vec<String>
},
Private
Partial { visible_to_fqns: Vec<String> },
Private,
}
pub struct DmNamespace {
pub name: String,
pub dependencies: Vec<String>,
pub visibility: NamespaceVisibility
pub visibility: NamespaceVisibility,
}

View File

@ -415,10 +415,14 @@ InterfaceDefaultOperatorFunction:
# Function Bodies
FunctionBody:
type: leaf_enum
rules:
- FunctionAliasBody
- FunctionEqualsBody
- FunctionBlockBody
- FunctionAliasBody:
child: true
- FunctionEqualsBody:
child: true
- FunctionBlockBody:
child: true
FunctionEqualsBody:
children:
- expression
@ -460,14 +464,22 @@ Member:
# Statements
Statement:
type: leaf_enum
rules:
- VariableDeclaration
- AssignmentStatement
- ExpressionStatement
- UseStatement
- IfStatement
- WhileStatement
- ForStatement
- VariableDeclaration:
child: true
- AssignmentStatement:
child: true
- ExpressionStatement:
child: true
- UseStatement:
child: true
- IfStatement:
child: true
- WhileStatement:
child: true
- ForStatement:
child: true
VariableDeclaration:
children:
- let_kw:
@ -701,17 +713,23 @@ ObjectIndex:
children:
- expression
PrimaryExpression:
type: leaf_enum
rules:
- Literal
- FullyQualifiedName
- Closure
- ParenthesizedExpression
- Literal:
child: true
- FullyQualifiedName:
child: true
- Closure:
child: true
- ParenthesizedExpression:
child: true
ParenthesizedExpression:
children:
- expression
# Calls
Call:
type: leaf_enum
rules:
- ParenthesesCall
- NonParenthesesCall
@ -762,38 +780,46 @@ ClosureParameter:
# Literals
Literal:
type: leaf_enum
rules:
- NumberLiteral
- StringLiteral
- BooleanLiteral
- NumberLiteral:
child: true
- StringLiteral:
child: true
- BooleanLiteral:
child: true
NumberLiteral:
type: leaf_enum
rules:
- DoubleLiteral
- LongLiteral
- IntLiteral
- DoubleLiteral:
child: true
- LongLiteral:
child: true
- IntLiteral:
child: true
IntLiteral:
children:
- number_base
- literal:
build:
type: i32
LongLiteral:
children:
- number_base
- literal:
build:
type: i64
DoubleLiteral:
type: leaf_struct
children:
- literal:
build:
type: f64
NumberBase:
type: leaf_enum
rules:
- BinaryBase
- HexadecimalBase
- DecimalBase
- BinaryBase:
child: true
- HexadecimalBase:
child: true
- DecimalBase:
child: true
DecimalBase:
type: leaf_struct
children:
- literal:
build:
@ -802,6 +828,7 @@ BinaryBase:
children:
- binary_digits
BinaryDigits:
type: leaf_struct
children:
- literal:
build:
@ -810,15 +837,20 @@ HexadecimalBase:
children:
- hexadecimal_digits
HexadecimalDigits:
type: leaf_struct
children:
- literal:
build:
type: string
StringLiteral:
type: leaf_enum
rules:
- SingleQuoteString
- DoubleQuoteString
- BacktickString
- SingleQuoteString:
child: true
- DoubleQuoteString:
child: true
- BacktickString:
child: true
SingleQuoteString:
children:
- string_inner:
@ -832,11 +864,13 @@ DoubleQuoteString:
rule: DStringExpression
vec: true
StringInner:
type: leaf_struct
children:
- literal:
build:
type: string
DStringInner:
type: leaf_struct
children:
- literal:
build:
@ -853,11 +887,13 @@ BacktickString:
rule: DStringExpression
vec: true
BacktickInner:
type: leaf_struct
children:
- literal:
build:
type: string
BooleanLiteral:
type: leaf_struct
children:
- literal:
build:

View File

@ -5,7 +5,11 @@ pub struct IndentWriter {
}
impl IndentWriter {
pub fn new(start_level: usize, indent_string: &str, out: Box<dyn std::io::Write>) -> IndentWriter {
pub fn new(
start_level: usize,
indent_string: &str,
out: Box<dyn std::io::Write>,
) -> IndentWriter {
IndentWriter {
indent_level: start_level,
indent_string: indent_string.to_string(),

View File

@ -1,7 +1,7 @@
#[derive(Debug, Clone)]
pub enum DvmConstant {
String(String),
Array(DvmConstantArray)
Array(DvmConstantArray),
}
#[derive(Debug, Clone)]
@ -13,5 +13,5 @@ pub enum DvmConstantArray {
USizes(Vec<usize>),
Booleans(Vec<bool>),
Strings(Vec<String>),
Arrays(Vec<DvmConstantArray>)
Arrays(Vec<DvmConstantArray>),
}

View File

@ -111,8 +111,8 @@ pub enum Instruction {
},
Return,
DumpState {
message: String
}
message: String,
},
}
#[derive(Debug, Clone)]

View File

@ -10,7 +10,7 @@ impl SourceCodeLocation {
SourceCodeLocation {
source_file_name: source_file_name.to_string(),
line,
col
col,
}
}
}

View File

@ -11,5 +11,5 @@ pub enum DvmType {
// Other
Object,
Array,
ConstantPointer
ConstantPointer,
}