Skip to content

Commit

Permalink
As much as I should do a conventional commit. I'm too lazy to individ…
Browse files Browse the repository at this point in the history
…ually add 63 files
  • Loading branch information
chorman0773 committed Nov 9, 2023
1 parent e788706 commit c8e2346
Show file tree
Hide file tree
Showing 45 changed files with 1,771 additions and 1,151 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ members = [
"codegen-x86","codegen-clever","codegen-w65",
"lc-binutils/arch-ops", "lc-binutils/binfmt",
"rust/interning-static-syms",
"rust/rust_mir_macro",
]
2 changes: 2 additions & 0 deletions c/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ license = "BSD-2-Clause-Patent"
[dependencies]
xlang = {path="../xlang"}
xlang_struct = {path="../xlang/xlang_struct"}
xlang_frontend = {path="../xlang/xlang_frontend"}
unicode-xid = "0.2.2"

[lib]
crate-type = ["cdylib"]
Expand Down
87 changes: 0 additions & 87 deletions c/src/analyze.rs
Original file line number Diff line number Diff line change
@@ -1,87 +0,0 @@
use crate::parse::{
BaseType, Declaration, Expression, FullType, Initializer, Pointer, PrimitiveType, Statement,
Type,
};
use std::collections::HashMap;

#[allow(clippy::missing_const_for_fn)] // b/c 1.54.0 doesn't support panic in const fns
fn diagnostic() -> ! {
panic!(\\_(ツ)_/¯ Sema error");
}

fn recurse_expr(expr: &mut Expression, ids_to_types: &HashMap<String, FullType>) {
match expr {
Expression::FunctionCall { callee, args, ty } => {
recurse_expr(callee, ids_to_types);
for arg in args {
recurse_expr(arg, ids_to_types);
}
*ty = Some(match callee.get_type() {
Some(FullType {
inner:
Type {
base: BaseType::Function { ret, .. },
..
},
..
}) => FullType {
inner: (**ret).clone(),
pointer: None,
}, // TODO: Support pointers
Some(_) => diagnostic(),
None => unreachable!(),
});
}
Expression::Identifier { id, ty } => {
*ty = Some(ids_to_types[id].clone());
}
Expression::String { ty, .. } => {
*ty = Some(FullType {
inner: Type {
base: BaseType::Primitive(PrimitiveType::Char),
constant: true,
},
pointer: Some(Pointer {
constant: false,
restrict: false,
sub_ptr: None,
}),
});
}
}
}

pub fn analyze(file: &mut Vec<Declaration>) {
let mut ids_to_types = HashMap::new();
for decl in file {
if ids_to_types.contains_key(&decl.name) {
if ids_to_types[&decl.name]
!= (FullType {
inner: decl.ty.clone(),
pointer: decl.pointer.clone(),
})
{
diagnostic();
}
} else {
ids_to_types.insert(
decl.name.clone(),
FullType {
inner: decl.ty.clone(),
pointer: decl.pointer.clone(),
},
);
}
if let Some(init) = &mut decl.initializer {
match init {
Initializer::Expression(expr) => recurse_expr(expr, &ids_to_types),
Initializer::Function(func) => {
for stmt in func {
let Statement::Expression(expr) = stmt;
recurse_expr(expr, &ids_to_types);
}
}
}
}
}
}
184 changes: 0 additions & 184 deletions c/src/lex.rs
Original file line number Diff line number Diff line change
@@ -1,184 +0,0 @@
use core::iter::Peekable;

// 6.4.1: Keywords
static KEYWORDS: [&str; 47] = [
"auto",
"break",
"case",
"char",
"const",
"continue",
"default",
"do",
"double",
"else",
"enum",
"extern",
"float",
"for",
"goto",
"if",
"inline",
"int",
"long",
"register",
"restrict",
"return",
"short",
"signed",
"sizeof",
"static",
"struct",
"switch",
"typedef",
"union",
"unsigned",
"void",
"volatile",
"while",
"_Alignas",
"_Alignof",
"_Atomic",
"_Bool",
"_Complex",
"_Decimal128",
"_Decimal32",
"_Decimal64",
"_Generic",
"_Imaginary",
"_Noreturn",
"_Static_assert",
"_Thread_local",
];

// 6.4.4.4: Character constants
#[allow(dead_code)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum CharacterEncoding {
Integer,
Utf,
Wide,
}

// 6.4.4: Constants
#[allow(dead_code)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Constant {
Integer(String),
Floating(String),
Character(CharacterEncoding, String),
}

// 6.4.5: String literals
#[allow(dead_code)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum StringEncoding {
Character,
Utf,
Wide,
}

// 6.4: Lexical elements
#[allow(dead_code)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Token {
Keyword(String),
Identifier(String),
Constant(Constant),
StringLiteral(StringEncoding, String),
Punctuator(String),
}

pub fn diagnostic() -> ! {
eprintln!("Something didn't work. Better messages coming soon.");
std::process::exit(1);
}

pub fn string_literal<I: Iterator<Item = char>>(file: &mut Peekable<I>) -> String {
if file.next() == Some('"') {
let mut result = String::new();
let mut hit_end_quote = false;
while let Some(&c) = file.peek() {
if c == '\\' {
todo!();
} else if c == '"' {
file.next();
hit_end_quote = true;
break;
} else {
file.next();
result.push(c);
}
}
if !hit_end_quote {
diagnostic()
}
result
} else {
diagnostic()
}
}

pub fn lex<I: Iterator<Item = char>>(file: &mut I) -> Vec<Token> {
let mut file = file.peekable();
let mut result = Vec::new();
while let Some(&c) = file.peek() {
match c {
x if x.is_alphabetic() || x == '_' => {
// 6.4.2: Identifiers
file.next();
let mut token = String::from(x);
while let Some(&c) = file.peek() {
if c.is_alphabetic() || c == '_' || c.is_numeric() {
file.next();
token.push(c);
} else {
break;
}
}
if KEYWORDS.contains(&&*token) {
result.push(Token::Keyword(token));
} else {
let c = file.peek();
if c == Some(&'\'') {
todo!();
} else if c == Some(&'"') {
let string = string_literal(&mut file);
let encoding = match &*token {
"u8" => StringEncoding::Utf,
"L" | "u" | "U" => StringEncoding::Wide,
_ => diagnostic(),
};
result.push(Token::StringLiteral(encoding, string));
} else {
result.push(Token::Identifier(token));
}
}
}
x if x.is_whitespace() => {
file.next();
}
'[' | ']' | '(' | ')' | '{' | '}' | '~' | '?' | ';' | ',' => {
file.next();
result.push(Token::Punctuator(String::from(c)));
}
'*' | '!' | '%' => {
file.next();
if file.peek() == Some(&'=') {
file.next();
result.push(Token::Punctuator(String::from(c) + "="));
} else {
result.push(Token::Punctuator(String::from(c)));
}
}
'"' => {
result.push(Token::StringLiteral(
StringEncoding::Character,
string_literal(&mut file),
));
}
_ => todo!("{}", c),
}
}
result
}
Loading

0 comments on commit c8e2346

Please sign in to comment.