Skip to content
Draft
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
447 changes: 445 additions & 2 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ clap = { version = "4.5.58", features = ["derive", "wrap_help", "string", "cargo
cliclack = "0.3.9"
console = "0.16.2"
vfs = "0.12.2"
lalrpop-util = "0.23.1"
inkwell = { version = "0.9.0", features = ["llvm22-1"] }

[dev-dependencies]
pretty_assertions = "1.4.1"
rand = "0.10.0"

[build-dependencies]
lalrpop = "0.23.1"
24 changes: 7 additions & 17 deletions src/errors/test.rs → build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,11 @@
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

use crate::errors::{GenericError, NotImplementedError};
use pretty_assertions::assert_eq;

#[test]
fn it_stores_message() {
assert_eq!(
"My error message",
GenericError::new("My error message").to_string()
);
}

#[test]
fn it_tells_feature_is_not_implemented() {
assert_eq!(
"foo is not yet implemented",
NotImplementedError::new("foo").to_string()
);
fn main() {
lalrpop::Configuration::new()
.set_in_dir("./src")
.set_out_dir("./src")
.force_build(true)
.process()
.unwrap()
}
16 changes: 15 additions & 1 deletion fil.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
{ rustPlatform, lib }:
{
rustPlatform,
lib,
libllvm,
libffi,
libxml2,
zlib,
}:

rustPlatform.buildRustPackage rec {
name = "fil";
Expand All @@ -8,6 +15,13 @@ rustPlatform.buildRustPackage rec {
lockFile = "${src}/Cargo.lock";
};

nativeBuildInputs = [ libllvm ];
buildInputs = [
libffi
libxml2
zlib
];

doCheck = true;
checkPhase = ''
runHook preCheck
Expand Down
8 changes: 4 additions & 4 deletions flake.lock

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

21 changes: 18 additions & 3 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.11";
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";

treefmt-nix = {
url = "github:numtide/treefmt-nix/28b19c5844cc6e2257801d43f2772a4b4c050a1b";
Expand All @@ -22,9 +22,14 @@
eachSystem = nixpkgs.lib.genAttrs (import systems);
pkgs = eachSystem (system: import nixpkgs { inherit system; });

fil-version = (builtins.fromTOML (builtins.readFile ./Cargo.toml)).package.version;
fil-version = (fromTOML (builtins.readFile ./Cargo.toml)).package.version;

fil-package = eachSystem (system: pkgs.${system}.callPackage ./fil.nix { });
fil-package = eachSystem (
system:
pkgs.${system}.callPackage ./fil.nix {
libllvm = pkgs.${system}.llvmPackages_22.libllvm;
}
);
rpm-package = eachSystem (
system:
pkgs.${system}.callPackage ./tools/package/rpm.nix {
Expand Down Expand Up @@ -56,12 +61,22 @@
packages = with pkgs.${system}; [
git
rustup
llvmPackages_22.libllvm
libffi
libxml2
(import ./tools/nix/treefmt.nix {
inherit treefmt-nix;
pkgs = pkgs.${system};
})
];

LD_LIBRARY_PATH =
with pkgs.${system};
lib.makeLibraryPath [
libffi
stdenv.cc.cc
];

shellHook = ''
export ROOT_DIR=$(git rev-parse --show-toplevel)
export PATH="$PATH:$ROOT_DIR/tools/bin"
Expand Down
1 change: 1 addition & 0 deletions src/build/grammar/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
grammar.rs
50 changes: 50 additions & 0 deletions src/build/grammar/ast.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// fil
// Copyright (C) 2026 - Present fil contributors
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

use std::fmt::{Debug, Formatter};

pub enum Expr {
Number(u32),
Op(Box<Expr>, Opcode, Box<Expr>),
}

pub enum Opcode {
Mul,
Div,
Add,
Sub,
}

impl Debug for Expr {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Expr::Number(n) => write!(f, "{n:?}"),
Expr::Op(l, op, r) => write!(f, "({l:?} {op:?} {r:?})"),
}
}
}

impl Debug for Opcode {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Opcode::Mul => write!(f, "*"),
Opcode::Div => write!(f, "/"),
Opcode::Add => write!(f, "+"),
Opcode::Sub => write!(f, "-"),
}
}
}
33 changes: 33 additions & 0 deletions src/build/grammar/grammar.lalrpop
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::str::FromStr;
use crate::build::grammar::ast::{Expr, Opcode};

grammar;

pub Expr: Box<Expr> = {
Expr ExprOp Factor => Box::new(Expr::Op(<>)),
Factor,
};

ExprOp: Opcode = {
"+" => Opcode::Add,
"-" => Opcode::Sub,
};

Factor: Box<Expr> = {
Factor FactorOp Term => Box::new(Expr::Op(<>)),
Term,
};

FactorOp: Opcode = {
"*" => Opcode::Mul,
"/" => Opcode::Div,
};

Term: Box<Expr> = {
Num => Box::new(Expr::Number(<>)),
"(" <Expr> ")"
};

Num: u32 = {
r"[0-9]+" => u32::from_str(<>).unwrap()
};
90 changes: 90 additions & 0 deletions src/build/grammar/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// fil
// Copyright (C) 2026 - Present fil contributors
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

use crate::build::grammar::ast::Expr;
use crate::fault;
use crate::fault::Fault;
use parse_error_formatter::format_parse_error;

pub mod ast;
pub mod grammar;
mod parse_error_formatter;

pub fn parse_file(main_source_file: &vfs::path::VfsPath) -> fault::Result<Box<Expr>> {
main_source_file
.read_to_string()
.map_err(|error| Fault::from_error(Box::from(error)))
.and_then(|content| {
grammar::ExprParser::new()
.parse(content.as_str())
.map_err(|error| Fault::from_message(format_parse_error(error, &content).as_str()))
})
}

#[cfg(test)]
mod test {
use crate::build::grammar::grammar;
use crate::build::grammar::parse_file;
use pretty_assertions::{assert_eq, assert_str_eq};
use vfs::{MemoryFS, VfsPath};

#[test]
fn test_grammar() {
let expr = grammar::ExprParser::new().parse("22 * 44 + 66").unwrap();
assert_str_eq!(&format!("{:?}", expr), "((22 * 44) + 66)");
}

#[test]
fn test_parse_file() {
let root = VfsPath::new(MemoryFS::new());
root.join("src").unwrap().create_dir().unwrap();
let source_file = root.join("src/main.rs").unwrap();
source_file.create_file().unwrap();
source_file
.append_file()
.unwrap()
.write_fmt(format_args!("1 + 3 * 12 -4"))
.unwrap();

let expr = parse_file(&source_file).unwrap();
assert_str_eq!(&format!("{:?}", expr), "((1 + (3 * 12)) - 4)");
}

#[test]
fn test_parse_file_err() {
let root = VfsPath::new(MemoryFS::new());
root.join("src").unwrap().create_dir().unwrap();
let source_file = root.join("src/main.rs").unwrap();
source_file.create_file().unwrap();
source_file
.append_file()
.unwrap()
.write_fmt(format_args!("1 + hello"))
.unwrap();

let result = parse_file(&source_file);
assert_eq!(result.is_err(), true);
assert_str_eq!(
format!("{}", result.err().unwrap()),
"Invalid token at line 1:

1 | 1 + hello
^
"
);
}
}
Loading