Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Rust
name: Compile (No bridges)

on:
pull_request:
Expand All @@ -15,4 +15,4 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Build
run: cargo build --verbose
run: cargo build -p compiler_main
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion compiler/ast/src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ use crate::tree::ASTTreeNode;
#[derive(Debug)]
pub struct ParserCtx {
pub map: HashMap<String, Box<ASTTreeNode>>,
pub uses: Vec<Box<ASTTreeNode>>,
pub iter_order: Vec<String>
}

impl ParserCtx {
pub fn new() -> Self {
return ParserCtx { map: HashMap::new(), iter_order: Vec::new() }
return ParserCtx { map: HashMap::new(), iter_order: Vec::new(), uses: vec![] }
}

pub fn insert(&mut self, name: String, node: Box<ASTTreeNode>) -> bool {
Expand Down
3 changes: 3 additions & 0 deletions compiler/ast/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub enum ASTTreeNodeKind {
IntegerLit { val: i128, hash: u64 },
StringLit(String),

UseStatement { shards: Vec<HashedString>, use_clauses: Vec<HashedString> },

ThisStructParam,

UnwrapCondition { original: Box<ASTTreeNode>, target_type: ASTType, unsafe_unwrap: bool, target_var: Option<HashedString> },
Expand Down Expand Up @@ -166,6 +168,7 @@ impl Display for ASTTreeNode {
impl Display for ASTTreeNodeKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = match self {
Self::UseStatement { .. } => "use statement",
Self::UnwrapCondition { .. } | Self::UnwrapValue { .. } => "unwrap",
Self::IntegerLit { .. } => "integer literal",
Self::StringLit(_) => "string literal",
Expand Down
8 changes: 7 additions & 1 deletion compiler/ast_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! The parser for the Quickfall AST
//!

use ast::ctx::ParserCtx;
use ast::{ctx::ParserCtx, tree::ASTTreeNodeKind};
use diagnostics::{DiagnosticResult, builders::make_unexpected_simple_error};
use lexer::token::{LexerToken, LexerTokenType};

Expand All @@ -19,6 +19,7 @@ pub mod variables;
pub mod types;
pub mod arrays;
pub mod unwraps;
pub mod use_statements;

pub fn parse_ast_ctx(tokens: &Vec<LexerToken>) -> DiagnosticResult<ParserCtx> {
let mut ind = 0;
Expand All @@ -28,6 +29,11 @@ pub fn parse_ast_ctx(tokens: &Vec<LexerToken>) -> DiagnosticResult<ParserCtx> {
while tokens[ind].tok_type != LexerTokenType::EndOfFile {
let node = parse_ast_node(tokens, &mut ind)?;

if let ASTTreeNodeKind::UseStatement { .. } = node.kind {
ctx.uses.push(node);
continue;
}

if !node.kind.is_tree_permissible() {
return Err(make_unexpected_simple_error(&*node, &node).into())
}
Expand Down
8 changes: 6 additions & 2 deletions compiler/ast_parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use compiler_utils::hash::HashedString;
use diagnostics::{DiagnosticResult, builders::{make_unexpected_simple_error, make_unused_variable}};
use lexer::token::{LexerToken, LexerTokenType};

use crate::{control::{for_loop::parse_for_loop, if_else::parse_if_statement, while_block::parse_while_block}, functions::{parse_function_call, parse_function_declaraction, returns::parse_function_return_statement, shadow::parse_shadow_function_declaration}, structs::{enums::parse_enum_declaration, parse_type_declaration}, value::parse_ast_value_post_l, variables::{decl::parse_variable_declaration, static_decl::parse_static_variable_declaration}};
use crate::{control::{for_loop::parse_for_loop, if_else::parse_if_statement, while_block::parse_while_block}, functions::{parse_function_call, parse_function_declaraction, returns::parse_function_return_statement, shadow::parse_shadow_function_declaration}, structs::{enums::parse_enum_declaration, parse_type_declaration}, use_statements::parse_use_statement, value::parse_ast_value_post_l, variables::{decl::parse_variable_declaration, static_decl::parse_static_variable_declaration}};

/// Parses an AST node outside of any other node.
///
Expand Down Expand Up @@ -38,12 +38,16 @@ pub fn parse_ast_node(tokens: &Vec<LexerToken>, ind: &mut usize) -> DiagnosticRe
return parse_enum_declaration(tokens, ind);
},

LexerTokenType::Use => {
return parse_use_statement(tokens, ind);
}

_ => return Err(make_unexpected_simple_error(&tokens[*ind], &tokens[*ind].tok_type).into())
}
}

/// Parses an AST node inside of another compatible node (functions, control bodies)
pub fn parse_ast_node_in_body(tokens: &Vec<LexerToken>, ind: &mut usize) -> DiagnosticResult<Box<ASTTreeNode>> {
pub fn parse_ast_node_in_body(tokens: &Vec<LexerToken>, ind: &mut usize) -> DiagnosticResult<Box<ASTTreeNode>> {
match &tokens[*ind].tok_type {

LexerTokenType::Var => {
Expand Down
4 changes: 2 additions & 2 deletions compiler/ast_parser/src/structs/enums.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use ast::{tree::{ASTTreeNode, ASTTreeNodeKind}, types::ASTType};
use compiler_utils::hash::HashedString;
use diagnostics::{DiagnosticResult, diagnostic::Diagnostic};
use diagnostics::{DiagnosticResult};
use lexer::token::{LexerToken, LexerTokenType};

use crate::{functions::parse_function_declaraction, structs::members::parse_types_field_member, types::{parse_type_generic, parse_type_parameters_declaration}};
use crate::{functions::parse_function_declaraction, structs::members::parse_types_field_member, types::{parse_type_parameters_declaration}};

pub fn parse_enum_entry(tokens: &Vec<LexerToken>, ind: &mut usize) -> DiagnosticResult<Box<ASTTreeNode>> {
let start = tokens[*ind].pos.clone();
Expand Down
44 changes: 44 additions & 0 deletions compiler/ast_parser/src/use_statements.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use ast::tree::{ASTTreeNode, ASTTreeNodeKind};
use compiler_utils::hash::HashedString;
use diagnostics::DiagnosticResult;
use lexer::token::{LexerToken, LexerTokenType};

pub fn parse_use_statement(tokens: &Vec<LexerToken>, ind: &mut usize) -> DiagnosticResult<Box<ASTTreeNode>> {
let start = tokens[*ind].pos.clone();

*ind += 1;

let mut steps = vec![];
let mut uses = vec![];

while tokens[*ind].is_keyword() {
let kwd = tokens[*ind].expects_keyword()?;
*ind += 1;

steps.push(HashedString::new(kwd.0));

tokens[*ind].expects(LexerTokenType::Collon)?;
*ind += 1;
}

tokens[*ind].expects(LexerTokenType::ArrayOpen)?;
*ind += 1;

while tokens[*ind].is_keyword() {
let kwd = tokens[*ind].expects_keyword()?;
*ind += 1;

uses.push(HashedString::new(kwd.0));

if tokens[*ind].tok_type == LexerTokenType::ArrayClose {
break;
}

tokens[*ind].expects(LexerTokenType::Comma)?;
*ind += 1;
}

*ind += 1;

return Ok(Box::new(ASTTreeNode::new(ASTTreeNodeKind::UseStatement { shards: steps, use_clauses: uses }, start, tokens[*ind].get_end_pos())))
}
2 changes: 1 addition & 1 deletion compiler/astoir_hir/src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use std::collections::HashMap;

use compiler_typing::{enums::{RawEnumEntryContainer, RawEnumTypeContainer}, raw::RawType, references::TypeReference, storage::{BOOLEAN_TYPE, STATIC_STR}, structs::RawStructTypeContainer, transmutation::array::can_transmute_inner, tree::Type};
use compiler_typing::{enums::{RawEnumTypeContainer}, raw::RawType, references::TypeReference, structs::RawStructTypeContainer, transmutation::array::can_transmute_inner, tree::Type};
use compiler_utils::{Position, hash::SelfHash};
use diagnostics::{DiagnosticSpanOrigin, builders::{make_diff_type, make_diff_type_val}, diagnostic::{Diagnostic, Span, SpanKind, SpanPosition}, unsure_panic};
use lexer::toks::{comp::ComparingOperator, math::MathOperator};
Expand Down
4 changes: 3 additions & 1 deletion compiler/astoir_hir_lowering/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ astoir_hir = { path = "../astoir_hir" }
compiler_typing = { path = "../compiler_typing" }
ast = { path = "../ast" }
diagnostics = { path = "../diagnostics" }
compiler_utils = { path = "../compiler_utils" }
compiler_utils = { path = "../compiler_utils" }
lexer = { path = "../lexer" }
ast_parser = { path = "../ast_parser" }
2 changes: 1 addition & 1 deletion compiler/astoir_hir_lowering/src/bools.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ast::tree::{ASTTreeNode, ASTTreeNodeKind};
use astoir_hir::{ctx::{HIRBranchedContext, HIRContext}, nodes::{HIRNode, HIRNodeKind}};
use compiler_typing::{raw::RawType, storage::BOOLEAN_TYPE, tree::Type};
use compiler_typing::{raw::RawType, tree::Type};
use diagnostics::{DiagnosticResult};

use crate::values::lower_ast_value;
Expand Down
2 changes: 1 addition & 1 deletion compiler/astoir_hir_lowering/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub fn lower_ast_enum_entry(context: &mut HIRContext, node: Box<ASTTreeNode>, co
}

pub fn lower_ast_enum(context: &mut HIRContext, node: Box<ASTTreeNode>) -> DiagnosticResult<Box<HIRNode>> {
if let ASTTreeNodeKind::EnumDeclaration { name, entries, functions, type_params } = node.kind.clone() {
if let ASTTreeNodeKind::EnumDeclaration { name, entries, functions: _, type_params } = node.kind.clone() {
let mut container = RawEnumTypeContainer::new(context.type_storage.types.vals.len(), type_params);

for entry in entries {
Expand Down
7 changes: 6 additions & 1 deletion compiler/astoir_hir_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use ast::{ctx::ParserCtx, tree::{ASTTreeNode, ASTTreeNodeKind}};
use astoir_hir::{ctx::{HIRBranchedContext, HIRContext}, nodes::{HIRNode, HIRNodeKind}};
use diagnostics::{DiagnosticResult, DiagnosticSpanOrigin, move_current_diagnostic_pos};

use crate::{arrays::lower_ast_array_modify, control::{lower_ast_for_block, lower_ast_if_statement, lower_ast_while_block}, enums::lower_ast_enum, func::{lower_ast_function_call, lower_ast_function_declaration, lower_ast_shadow_function_declaration}, math::lower_ast_math_operation, structs::lower_ast_struct_declaration, values::lower_ast_value, var::{lower_ast_variable_assign, lower_ast_variable_declaration}};
use crate::{arrays::lower_ast_array_modify, control::{lower_ast_for_block, lower_ast_if_statement, lower_ast_while_block}, enums::lower_ast_enum, func::{lower_ast_function_call, lower_ast_function_declaration, lower_ast_shadow_function_declaration}, math::lower_ast_math_operation, structs::lower_ast_struct_declaration, uses::handle_ast_use_statement, values::lower_ast_value, var::{lower_ast_variable_assign, lower_ast_variable_declaration}};

pub mod literals;
pub mod var;
Expand All @@ -16,6 +16,7 @@ pub mod structs;
pub mod arrays;
pub mod unwraps;
pub mod enums;
pub mod uses;

pub fn lower_ast_body_node(context: &mut HIRContext, curr_ctx: &mut HIRBranchedContext, node: Box<ASTTreeNode>) -> DiagnosticResult<Box<HIRNode>> {
move_current_diagnostic_pos(node.get_pos());
Expand Down Expand Up @@ -106,6 +107,10 @@ pub fn lower_ast_toplevel(context: &mut HIRContext, node: Box<ASTTreeNode>) -> D
pub fn lower_ast(ctx: ParserCtx) -> DiagnosticResult<HIRContext> {
let mut hir_ctx = HIRContext::new();

for u in ctx.uses {
handle_ast_use_statement(&mut hir_ctx, u)?;
}

for s in ctx.iter_order {
let k = ctx.map[&s].clone();

Expand Down
8 changes: 4 additions & 4 deletions compiler/astoir_hir_lowering/src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use std::collections::HashMap;

use ast::tree::{ASTTreeNode, ASTTreeNodeKind};
use astoir_hir::{ctx::{HIRBranchedContext, HIRContext}, nodes::{HIRNode, HIRNodeKind}, structs::HIRStructContainer};
use compiler_typing::{raw::RawType, structs::RawStructTypeContainer, tree::Type};
use compiler_utils::{hash::{HashedString, SelfHash}, utils::indexed::IndexStorage};
use diagnostics::{DiagnosticResult, builders::{make_already_in_scope, make_cannot_find_type, make_struct_init_missing_field, make_struct_missing_field}};
use compiler_typing::{raw::RawType, structs::RawStructTypeContainer};
use compiler_utils::utils::indexed::IndexStorage;
use diagnostics::{DiagnosticResult, builders::make_already_in_scope};

use crate::{lower_ast_body, types::{lower_ast_type, lower_ast_type_struct}, values::lower_ast_value};
use crate::{lower_ast_body, types::lower_ast_type_struct, values::lower_ast_value};

fn lower_ast_struct_member(context: &mut HIRContext, node: Box<ASTTreeNode>, container: &mut RawStructTypeContainer) -> DiagnosticResult<bool> {
if let ASTTreeNodeKind::StructFieldMember { name, member_type } = node.kind.clone() {
Expand Down
2 changes: 1 addition & 1 deletion compiler/astoir_hir_lowering/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ast::types::ASTType;
use astoir_hir::ctx::HIRContext;
use compiler_typing::{TypeParamType, raw::RawType, references::TypeReference, structs::RawStructTypeContainer, tree::Type};
use compiler_typing::{TypeParamType, raw::RawType, references::TypeReference, tree::Type};
use compiler_utils::hash::HashedString;
use diagnostics::{DiagnosticResult, DiagnosticSpanOrigin, builders::{make_cannot_find_type, make_diff_size_specifiers, make_diff_type_specifiers, make_req_type_kind}};

Expand Down
Loading