Compare commits
3 Commits
18551af61a
...
36955295bc
Author | SHA1 | Date | |
---|---|---|---|
![]() |
36955295bc | ||
![]() |
d566fc046a | ||
![]() |
80c3aa9f19 |
@ -7,6 +7,10 @@ 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"] }
|
||||
|
4
dm_std_lib/std/core/ns.yaml
Normal file
4
dm_std_lib/std/core/ns.yaml
Normal file
@ -0,0 +1,4 @@
|
||||
ns
|
||||
name: core
|
||||
patch: false
|
||||
vis: pub
|
@ -4,5 +4,4 @@ pub int String {
|
||||
bytes: Array<Byte>
|
||||
}
|
||||
|
||||
#[internal]
|
||||
impl StringImpl(bytes) : String
|
||||
|
163
src/ast/mod.rs
163
src/ast/mod.rs
@ -24,7 +24,8 @@ pub enum Declaration {
|
||||
identifier: Identifier,
|
||||
is_extern: bool,
|
||||
is_public: bool,
|
||||
type_declaration: TypeDeclaration
|
||||
type_declaration: TypeDeclaration,
|
||||
impl_ctor: Option<ImplCtor>
|
||||
},
|
||||
Module {
|
||||
identifier: Identifier,
|
||||
@ -36,8 +37,10 @@ pub enum Declaration {
|
||||
identifier: Identifier,
|
||||
is_extern: bool,
|
||||
is_public: bool,
|
||||
statement: Statement
|
||||
}
|
||||
statement: Statement
|
||||
},
|
||||
Prop(Prop),
|
||||
Field(Field)
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@ -89,6 +92,30 @@ 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 {
|
||||
@ -98,6 +125,59 @@ 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 {
|
||||
@ -163,14 +243,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
|
||||
@ -181,7 +261,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 => {
|
||||
@ -191,7 +271,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));
|
||||
@ -203,20 +283,57 @@ 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, pair: Pair<Rule>) -> Declaration {
|
||||
todo!()
|
||||
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_module(is_extern: bool, is_public: bool, pair: Pair<Rule>) -> Declaration {
|
||||
@ -240,7 +357,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 => {
|
||||
@ -252,6 +369,12 @@ 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)
|
||||
}
|
||||
}
|
||||
@ -265,8 +388,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 {
|
||||
@ -291,7 +414,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 => {
|
||||
@ -299,7 +422,7 @@ fn build_compilation_unit(pair: Pair<Rule>)-> CompilationUnit {
|
||||
}
|
||||
Rule::EOI => {} // ignore
|
||||
_ => panic!("Expected only namespace, declaration, or EOI rules, found: {}", pair)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CompilationUnit {
|
||||
|
10
src/bin/dmc/ast_dump.rs
Normal file
10
src/bin/dmc/ast_dump.rs
Normal file
@ -0,0 +1,10 @@
|
||||
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);
|
||||
}
|
34
src/bin/dmc/main.rs
Normal file
34
src/bin/dmc/main.rs
Normal file
@ -0,0 +1,34 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -15,9 +15,6 @@ 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");
|
||||
|
||||
|
@ -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 ) }
|
||||
declaration = { extern? ~ pub? ~ ( interface | implementation | module | function | prop | field ) }
|
||||
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? }
|
||||
implementation = { "impl" ~ identifier ~ generic_parameters_declaration? ~ impl_ctor? ~ extends_list? ~ ( "{" ~ declaration* ~ "}" )? }
|
||||
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,6 +37,9 @@ generic_argument = { fqn }
|
||||
args_list = { arg ~ ( "," ~ arg )* }
|
||||
arg = { identifier ~ ( ":" ~ "..."? ~ type )? }
|
||||
|
||||
prop = { identifier ~ ":" ~ type }
|
||||
field = { fld ~ identifier ~ ":" ~ type }
|
||||
|
||||
statement = { call_stmt }
|
||||
|
||||
call_stmt = { call_expr }
|
||||
|
Loading…
Reference in New Issue
Block a user