Skip to content

Commit

Permalink
Error System, Refactors (#75)
Browse files Browse the repository at this point in the history
- Separated WASM output into separate crate
- Basic Playground
- Easy to create errors
- Easy to create a new kind of error
- Error stack traces in debug mode
  • Loading branch information
A1Liu authored May 1, 2023
1 parent 14d2b49 commit 81feb04
Show file tree
Hide file tree
Showing 88 changed files with 661 additions and 172 deletions.
2 changes: 1 addition & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[alias]
wc = "check --target wasm32-unknown-unknown"
wasm = "build --target wasm32-unknown-unknown"

[build]
target-dir = ".cargo/target"
Expand Down
6 changes: 2 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
/.dist
/.cache
/.pkg
/.letsencrypt
/.next
/out

/lib/codespan-reporting/target
/letsencrypt
/node_modules
pkg
node_modules

/test.c
/a.out
68 changes: 49 additions & 19 deletions Cargo.lock

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

30 changes: 5 additions & 25 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,29 +1,9 @@
[package]
name = "tci"
version = "0.1.0"
authors = ["Albert Liu <[email protected]>"]
edition = "2021"
description = "Teaching C Interpreter"
license = "MIT"
[workspace]

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
wasm-bindgen = "0.2.70"
wee_alloc = "0.4.5"
lazy_static = "1.4.0"
soa_derive = "0.12.0"
codespan-reporting = "0.11.1"
serde = { version = "1.0.59", features = ["derive"] }
serde_json = "1.0.59"
clap = { version = "4.2.4", features = ["derive"] }
bitfield-struct = "0.3"
enum_derive = "0.1.7"
macro-attr = "0.2.0"

[dev-dependencies]
ntest = "0.9.0"
members = [
"compiler",
"tci-web"
]

[profile.dev]
opt-level = 0
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# C Compiler
# TCI - C Compiler for Students
The goals of this compiler are:

1. Provide better error messages for new programmers
Expand Down Expand Up @@ -52,4 +52,5 @@ In the UI:
- Translation of C standard to AST types - https://github.com/vickenty/lang-c/blob/master/src/ast.rs
- Compiler Architecture - https://scholarworks.iu.edu/dspace/handle/2022/24749
- Precedence climbing method - https://eli.thegreenplace.net/2012/08/02/parsing-expressions-by-precedence-climbing

- Monaco Editor Quick Fixes - https://stackoverflow.com/questions/57994101/show-quick-fix-for-an-error-in-monaco-editor
- Fuzzer to look into - https://github.com/rust-fuzz/afl.rs
21 changes: 21 additions & 0 deletions compiler/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "compiler"
version = "0.1.0"
authors = ["Albert Liu <[email protected]>"]
edition = "2021"
description = "Teaching C Interpreter"
license = "MIT"

[dependencies]
lazy_static = "1.4.0"
soa_derive = "0.12.0"
codespan-reporting = "0.11.1"
serde = { version = "1.0.59", features = ["derive"] }
serde_json = "1.0.59"
clap = { version = "4.2.4", features = ["derive"] }
bitfield-struct = "0.3"
enum_derive = "0.1.7"
macro-attr = "0.2.0"

[dev-dependencies]
ntest = "0.9.0"
38 changes: 15 additions & 23 deletions src/ast.rs → compiler/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ pub struct AstNode {
}

macro_attr! {
#[derive(Debug, Clone, Copy, EnumFromInner!)]
#[cfg_attr(all(debug_assertions), derive(Serialize, Deserialize))]
#[derive(Serialize, Deserialize, Debug, Clone, Copy, EnumFromInner!)]
#[serde(tag = "kind", content = "data")]
pub enum AstNodeKind {
Expr(AstExpr),
Statement(AstStatement),
Expand All @@ -31,12 +31,13 @@ pub enum AstNodeKind {
Specifier(AstSpecifier),
Declaration(AstDeclaration),
FunctionDefinition(AstFunctionDefinition),
EOF(()),
}
}

#[derive(Debug, Clone, Copy)]
#[cfg_attr(all(debug_assertions), derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct AstEof;

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum AstExpr {
IntLit, // data: i32
LongLit, // data: i64
Expand All @@ -61,8 +62,7 @@ pub enum AstExpr {
BinOpAssign(BinOp), // children: expression being assigned to, expression being assigned
}

#[derive(Debug, Clone, PartialEq, Hash, Eq, Copy)]
#[cfg_attr(all(debug_assertions), derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Hash, Eq, Copy, Serialize, Deserialize)]
pub enum BinOp {
Add,
Sub,
Expand All @@ -87,8 +87,7 @@ pub enum BinOp {
Comma,
}

#[derive(Debug, Clone, PartialEq, Hash, Eq, Copy)]
#[cfg_attr(all(debug_assertions), derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Hash, Eq, Copy, Serialize, Deserialize)]
pub enum UnaryOp {
Neg,
BoolNot,
Expand All @@ -105,15 +104,13 @@ pub enum UnaryOp {
/// struct a { int b; }
/// In the above, it would have children for each field
/// declaration, and a child for the identifier as well.
#[derive(Debug, Clone, Copy)]
#[cfg_attr(all(debug_assertions), derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum StructDeclaration {
Struct,
Union,
}

#[derive(Debug, Clone, Copy)]
#[cfg_attr(all(debug_assertions), derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum AstStatement {
Labeled, // data: label ; children: statement that is being labelled
CaseLabeled, // children: case value expression, statement that is being labelled
Expand All @@ -136,8 +133,7 @@ pub enum AstStatement {
/// `int *const a`, or the `[3]` part of `int b[3]`
///
/// Children: AstSpecifer for each type qualifier
#[derive(Debug, Clone, Copy)]
#[cfg_attr(all(debug_assertions), derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum AstDerivedDeclarator {
Pointer = 0,

Expand All @@ -162,8 +158,7 @@ pub enum AstDerivedDeclarator {
}

/// children: a AstDerivedDeclarator for each derived declarator
#[derive(Debug, Clone, Copy)]
#[cfg_attr(all(debug_assertions), derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum AstDeclarator {
Abstract,
/// data: Symbol
Expand All @@ -172,8 +167,7 @@ pub enum AstDeclarator {
NestedWithChild,
}

#[derive(Debug, Clone, Copy)]
#[cfg_attr(all(debug_assertions), derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum AstSpecifier {
Extern,
Static,
Expand Down Expand Up @@ -209,15 +203,13 @@ pub enum AstSpecifier {
/// int *i[1] = {NULL}; or something similar
///
/// Children: AstSpecifier for each specifier, AstStructDeclaration if necessary, an AstInitDeclarator for each declared variable
#[derive(Debug, Clone, Copy)]
#[cfg_attr(all(debug_assertions), derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct AstDeclaration;

/// A typical declaration; this is a stand-in for
/// int *i[1] = {NULL}; or something similar
///
/// Data: DeclarationSpecifiers
/// Children: AstSpecifier for each specifier, san AstDeclarator, and all the statements associated with the function
#[derive(Debug, Clone, Copy)]
#[cfg_attr(all(debug_assertions), derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct AstFunctionDefinition;
4 changes: 1 addition & 3 deletions src/main.rs → compiler/src/bin/test_runner.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
extern crate tci;
use clap::Parser;

/// Search for a pattern in a file and display the lines that contain it.
Expand All @@ -19,8 +18,7 @@ fn main() {
let test_case =
std::fs::read_to_string(&args.test_case).expect("file should exist and be a valid string");


let result = tci::run_test_code(&*test_case);
let result = compiler::api::run_test_code(&*test_case);

let text = result.test_case();

Expand Down
Loading

0 comments on commit 81feb04

Please sign in to comment.