Compare commits

..

No commits in common. "36955295bcfa6cafd5f6bcc9f47c70bd288f681f" and "18551af61a6a55338816217d22681028bcbf1dc9" have entirely different histories.

8 changed files with 28 additions and 202 deletions

View File

@ -7,10 +7,6 @@ edition = "2021"
name = "dm"
path = "src/bin/dvm/main.rs"
[[bin]]
name = "dmc"
path = "src/bin/dmc/main.rs"
[dependencies]
pest = "2.7.14"
clap = { version = "4.5.23", features = ["derive"] }

View File

@ -1,4 +0,0 @@
ns
name: core
patch: false
vis: pub

View File

@ -4,4 +4,5 @@ pub int String {
bytes: Array<Byte>
}
#[internal]
impl StringImpl(bytes) : String

View File

@ -24,8 +24,7 @@ pub enum Declaration {
identifier: Identifier,
is_extern: bool,
is_public: bool,
type_declaration: TypeDeclaration,
impl_ctor: Option<ImplCtor>
type_declaration: TypeDeclaration
},
Module {
identifier: Identifier,
@ -37,10 +36,8 @@ pub enum Declaration {
identifier: Identifier,
is_extern: bool,
is_public: bool,
statement: Statement
},
Prop(Prop),
Field(Field)
statement: Statement
}
}
#[derive(Debug)]
@ -92,30 +89,6 @@ pub struct Type {
generics: Vec<GenericArgument>
}
#[derive(Debug)]
pub struct ImplCtor {
args: Vec<ImplCtorArg>
}
#[derive(Debug)]
pub struct ImplCtorArg {
is_field: bool,
identifier: Identifier,
r#type: Option<Type>
}
#[derive(Debug)]
pub struct Prop {
identifier: Identifier,
r#type: Type
}
#[derive(Debug)]
pub struct Field {
identifier: Identifier,
r#type: Type
}
#[derive(Debug)]
pub enum Statement {
BlockStatement {
@ -125,59 +98,6 @@ pub enum Statement {
AssignmentStatement
}
fn build_field(field_pair: Pair<Rule>) -> Declaration {
todo!()
}
fn build_prop(prop_pair: Pair<Rule>) -> Declaration {
todo!()
}
fn build_impl_ctor_arg(impl_ctor_arg_pair: Pair<Rule>) -> ImplCtorArg {
let mut is_field = false;
let mut identifier: Option<Identifier> = None;
let mut r#type: Option<Type> = None;
for pair in impl_ctor_arg_pair.into_inner() {
match pair.as_rule() {
Rule::fld => {
is_field = true;
},
Rule::identifier => {
identifier = Some(build_identifier(pair));
},
Rule::r#type => {
r#type = Some(build_type(pair));
},
_ => panic!("Unexpected rule: {}", pair)
}
}
ImplCtorArg {
is_field,
identifier: identifier.unwrap(),
r#type
}
}
fn build_impl_ctor(impl_ctor_pair: Pair<Rule>) -> ImplCtor {
let impl_ctor_args_pair = impl_ctor_pair.into_inner().next().unwrap();
let mut args: Vec<ImplCtorArg> = vec![];
for pair in impl_ctor_args_pair.into_inner() {
match pair.as_rule() {
Rule::impl_ctor_arg => {
args.push(build_impl_ctor_arg(pair));
}
_ => panic!("Unexpected rule: {}", pair)
}
}
ImplCtor {
args
}
}
fn build_generic_argument(generic_argument_pair: Pair<Rule>) -> GenericArgument {
let fqn_pair = generic_argument_pair.into_inner().next().unwrap();
GenericArgument {
@ -243,14 +163,14 @@ fn build_generic_parameters_declaration(generic_parameters_declaration_pair: Pai
fn build_extends_list(extends_list_pair: Pair<Rule>) -> Vec<Type> {
let mut extensions: Vec<Type> = vec![];
for pair in extends_list_pair.into_inner() {
match pair.as_rule() {
Rule::r#type => {
extensions.push(build_type(pair));
extensions.push(build_type(pair));
}
_ => panic!("Expected only type rule. Found: {}", pair)
}
}
}
extensions
@ -261,7 +181,7 @@ fn build_interface(is_extern: bool, is_public: bool, interface_pair: Pair<Rule>)
let mut generic_parameters: Option<Vec<GenericParameter>> = None;
let mut extends: Option<Vec<Type>> = None;
let mut declarations: Vec<Declaration> = vec![];
for pair in interface_pair.into_inner() {
match pair.as_rule() {
Rule::identifier => {
@ -271,7 +191,7 @@ fn build_interface(is_extern: bool, is_public: bool, interface_pair: Pair<Rule>)
generic_parameters = Some(build_generic_parameters_declaration(pair));
}
Rule::extends_list => {
extends = Some(build_extends_list(pair));
extends = Some(build_extends_list(pair));
}
Rule::declaration => {
declarations.push(build_declaration(pair));
@ -283,57 +203,20 @@ fn build_interface(is_extern: bool, is_public: bool, interface_pair: Pair<Rule>)
}
}
Declaration::Interface {
identifier: identifier.unwrap(),
is_extern,
is_public,
Declaration::Interface {
identifier: identifier.unwrap(),
is_extern,
is_public,
type_declaration: TypeDeclaration::new(
generic_parameters.unwrap_or(vec![]),
extends.unwrap_or(vec![]),
extends.unwrap_or(vec![]),
declarations
)
}
}
fn build_implementation(is_extern: bool, is_public: bool, implementation_pair: Pair<Rule>) -> Declaration {
let mut identifier: Option<Identifier> = None;
let mut generic_parameters: Option<Vec<GenericParameter>> = None;
let mut impl_ctor: Option<ImplCtor> = None;
let mut extends: Option<Vec<Type>> = None;
let mut declarations: Vec<Declaration> = vec![];
for pair in implementation_pair.into_inner() {
match pair.as_rule() {
Rule::identifier => {
identifier = Some(build_identifier(pair));
},
Rule::generic_parameters_declaration => {
generic_parameters = Some(build_generic_parameters_declaration(pair));
},
Rule::impl_ctor => {
impl_ctor = Some(build_impl_ctor(pair));
},
Rule::extends_list => {
extends = Some(build_extends_list(pair));
},
Rule::declaration => {
declarations.push(build_declaration(pair));
}
_ => panic!("Unexpected rule: {}", pair)
}
}
Declaration::Implementation {
identifier: identifier.unwrap(),
is_extern,
is_public,
type_declaration: TypeDeclaration::new(
generic_parameters.unwrap_or(vec![]),
extends.unwrap_or(vec![]),
declarations
),
impl_ctor
}
fn build_implementation(is_extern: bool,is_public: bool, pair: Pair<Rule>) -> Declaration {
todo!()
}
fn build_module(is_extern: bool, is_public: bool, pair: Pair<Rule>) -> Declaration {
@ -357,7 +240,7 @@ fn build_declaration(declaration_pair: Pair<Rule>) -> Declaration {
Rule::r#pub => {
is_public = true;
}
Rule::interface => {
Rule::interface => {
declaration = Some(build_interface(is_extern, is_public, pair));
},
Rule::implementation => {
@ -369,12 +252,6 @@ fn build_declaration(declaration_pair: Pair<Rule>) -> Declaration {
Rule::function => {
declaration = Some(build_function(is_extern, is_public, pair));
},
Rule::prop => {
declaration = Some(build_prop(pair));
}
Rule::field => {
declaration = Some(build_field(pair));
}
_ => panic!("Expected only interface, implementation, module, or function rules; found {}", pair)
}
}
@ -388,8 +265,8 @@ fn build_identifier(pair: Pair<Rule>) -> Identifier {
name: String::from(pair.as_str())
}
}
_ => panic!("Expected an identifier.")
}
_ => panic!("Expected an identifier.")
}
}
fn build_fqn(fqn_pair: Pair<Rule>) -> Fqn {
@ -414,7 +291,7 @@ fn build_compilation_unit(pair: Pair<Rule>)-> CompilationUnit {
for pair in pair.into_inner() {
match pair.as_rule() {
Rule::namespace => {
let fqn_pair = pair.into_inner().next().unwrap();
let fqn_pair = pair.into_inner().next().unwrap();
namespace = Some(build_fqn(fqn_pair));
}
Rule::declaration => {
@ -422,7 +299,7 @@ fn build_compilation_unit(pair: Pair<Rule>)-> CompilationUnit {
}
Rule::EOI => {} // ignore
_ => panic!("Expected only namespace, declaration, or EOI rules, found: {}", pair)
}
}
}
CompilationUnit {

View File

@ -1,10 +0,0 @@
use std::path::PathBuf;
use deimos::ast::build_ast;
pub fn dump_ast(path: &PathBuf) {
let src = std::fs::read_to_string(path)
.expect(&format!("Could not read {:?}", path));
let ast = build_ast(&src);
println!("{:?}", ast);
}

View File

@ -1,34 +0,0 @@
mod ast_dump;
use std::path::PathBuf;
use ast_dump::dump_ast;
use clap::{Parser, Subcommand};
#[derive(Debug, Parser)]
#[command(name = "dmc")]
#[command(about = "Deimos Compiler", long_about = None)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Debug, Subcommand)]
enum Commands {
#[command(arg_required_else_help = true)]
AstDump {
paths: Vec<PathBuf>
}
}
fn main() {
let args = Cli::parse();
match args.command {
Commands::AstDump { paths } => {
for path in paths {
dump_ast(&path);
}
}
}
}

View File

@ -15,6 +15,9 @@ fn main() {
// - call the main fn
// fn main() { println "Hello, World!" }
let compilation_unit = build_ast("ns hello::test::bye\npub int Test<T> : Testing<T> {}");
println!("{:?}", compilation_unit);
// std/core/array lib
let mut array_lib = DmLib::new("std/core/array");

View File

@ -6,22 +6,22 @@ identifier = @{ identifier_start_char ~ identifier_char* }
identifier_start_char = { 'a'..'z' | 'A'..'Z' | "_" }
identifier_char = { 'a'..'z' | 'A'..'Z' | '0'..'9' | "_" }
declaration = { extern? ~ pub? ~ ( interface | implementation | module | function | prop | field ) }
declaration = { extern? ~ pub? ~ ( interface | implementation | module | function ) }
extern = { "extern" }
pub = { "pub" }
interface = { "int" ~ identifier ~ generic_parameters_declaration? ~ extends_list? ~ ( "{" ~ declaration* ~ "}" )? }
extends_list = { ":" ~ type ~ ( "+" ~ type )* }
implementation = { "impl" ~ identifier ~ generic_parameters_declaration? ~ impl_ctor? ~ extends_list? ~ ( "{" ~ declaration* ~ "}" )? }
implementation = { "impl" ~ identifier ~ generic_parameters_declaration? ~ impl_ctor? ~ extends_list? }
impl_ctor = { "(" ~ impl_ctor_args? ~ ")" }
impl_ctor_args = { impl_ctor_arg ~ ( "," ~ impl_ctor_arg )* }
impl_ctor_arg = { fld? ~ identifier ~ ( ":" ~ type )? }
impl_ctor_arg = { fld? ~ identifier ~ ":" ~ type }
fld = { "fld" }
module = { "mod" ~ identifier ~ "{" ~ declaration* ~ "}" }
property = { identifier ~ ( ":" ~ type )? }
property = { identifier ~ ":" ~ type }
function = { "fn" ~ generic_parameters_declaration? ~ identifier ~ "(" ~ args_list? ~ ")" ~ ( ":" ~ type )? ~ ( function_body | function_equals_body ) }
function_equals_body = { "=" ~ expression }
@ -37,9 +37,6 @@ generic_argument = { fqn }
args_list = { arg ~ ( "," ~ arg )* }
arg = { identifier ~ ( ":" ~ "..."? ~ type )? }
prop = { identifier ~ ":" ~ type }
field = { fld ~ identifier ~ ":" ~ type }
statement = { call_stmt }
call_stmt = { call_expr }