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,14 +518,14 @@ impl LeafStructChild {
pub fn new(name: &str, r#type: LeafStructChildType) -> Self {
Self {
name: name.to_string(),
r#type
r#type,
}
}
pub fn name(&self) -> &str {
&self.name
}
pub fn r#type(&self) -> &LeafStructChildType {
&self.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(
@ -229,7 +249,7 @@ fn make_leaf_struct_type(build_spec: &LeafStructBuildSpec) -> TokenStream {
}
})
.collect::<Vec<_>>();
quote! {
pub struct #type_ident {
#(#annotated_members),*

View File

@ -6,4 +6,4 @@ pub fn make_build_fn_name(s: &str) -> String {
pub fn make_build_pair(s: &str) -> String {
format!("{}_pair", s.to_case(Case::Snake))
}
}

View File

@ -17,7 +17,7 @@ fn generate_parser_tests(out_dir: &Path) -> io::Result<()> {
fn generate_ast_files(out_dir: &Path) -> io::Result<()> {
let gen_ast_dir = out_dir.join("src").join("ast");
fs::create_dir_all(&gen_ast_dir)?;
let ast_yaml = include_str!("src/parser/ast.yaml");
let build_specs = deserialize::deserialize_yaml_spec(ast_yaml);
let generated_files = generate_files(&build_specs);

View File

@ -46,7 +46,7 @@ pub fn generate_test_files(tests_dir: &Path) -> io::Result<ParserTestSuitesFile>
let test_suite = quote! {
mod #tests_mod_ident {
use super::*;
#(#tests)*
}
};
@ -59,7 +59,7 @@ pub fn generate_test_files(tests_dir: &Path) -> io::Result<ParserTestSuitesFile>
// generate main test_suites file
let test_suites = quote! {
use super::*;
#(#test_suites)*
};
let test_suites_file: File = syn::parse2(test_suites).unwrap();

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

@ -1,14 +1,14 @@
// mod name_analysis;
// mod p3;
// mod unparse;
//
//
// use std::path::PathBuf;
//
//
// use crate::name_analysis::name_analysis;
// use crate::p3::pretty_print_parse;
// use crate::unparse::unparse;
// use clap::{Parser, Subcommand};
//
//
// #[derive(Debug, Parser)]
// #[command(name = "dmc")]
// #[command(about = "Deimos Compiler", long_about = None)]
@ -16,7 +16,7 @@
// #[command(subcommand)]
// command: Commands,
// }
//
//
// #[derive(Debug, Subcommand)]
// enum Commands {
// #[command(arg_required_else_help = true)]
@ -30,7 +30,7 @@
// paths: Vec<PathBuf>,
// },
// }
//
//
// fn main() {
// let args = Cli::parse();
// match args.command {

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,9 +5,13 @@ 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_level: start_level,
indent_string: indent_string.to_string(),
out,
}
@ -24,7 +28,7 @@ impl IndentWriter {
pub fn write(&mut self, s: &str) -> std::io::Result<()> {
write!(self.out, "{}", s)
}
pub fn writeln(&mut self, s: &str) -> std::io::Result<()> {
self.write(&format!("{}\n", s))
}
@ -35,7 +39,7 @@ impl IndentWriter {
}
write!(self.out, "{}", s)
}
pub fn writeln_indented(&mut self, s: &str) -> std::io::Result<()> {
self.write_indented(&format!("{}\n", s))
}

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

@ -28,7 +28,7 @@ impl DvmFunction {
pub fn instructions(&self) -> &Vec<Instruction> {
&self.instructions
}
pub fn source_code_location(&self) -> &SourceCodeLocation {
&self.source_code_location
}

View File

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

View File

@ -21,7 +21,7 @@ impl DvmMethod {
implements,
}
}
pub fn fqn(&self) -> &str {
self.function.fqn()
}

View File

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

View File

@ -7,9 +7,9 @@ pub enum DvmType {
Double,
Boolean,
USize,
// Other
Object,
Array,
ConstantPointer
ConstantPointer,
}

View File

@ -70,7 +70,7 @@ impl DvmValue {
panic!("Expected DvmValue::Int, but found DvmValue::{:?}", self);
}
}
pub fn expect_int_or_else(&self, f: impl FnOnce(&Self) -> i32) -> i32 {
if let DvmValue::Int(i) = self {
*i