-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
As much as I should do a conventional commit. I'm too lazy to individ…
…ually add 63 files
- Loading branch information
1 parent
e788706
commit c8e2346
Showing
45 changed files
with
1,771 additions
and
1,151 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
Oops, something went wrong.