mirror of
https://github.com/Noratrieb/uwucc.git
synced 2026-01-14 16:45:07 +01:00
a bunch of stuff mostly
This commit is contained in:
parent
56e7f77a0d
commit
b9a2f939c4
29 changed files with 734 additions and 345 deletions
|
|
@ -3,9 +3,9 @@ use std::fmt::Debug;
|
|||
use bitflags::bitflags;
|
||||
use dbg_pls::DebugPls;
|
||||
|
||||
use crate::Spanned;
|
||||
use crate::{sym::Symbol, Spanned};
|
||||
|
||||
pub type Ident = Spanned<String>;
|
||||
pub type Ident = Spanned<Symbol>;
|
||||
|
||||
//
|
||||
// --- Expr
|
||||
|
|
@ -114,7 +114,7 @@ pub enum Stmt {
|
|||
},
|
||||
Compound(Vec<Spanned<Stmt>>),
|
||||
If {
|
||||
cond: Expr,
|
||||
cond: Spanned<Expr>,
|
||||
then: Vec<Spanned<Stmt>>,
|
||||
otherwise: Option<Vec<Spanned<Stmt>>>,
|
||||
},
|
||||
|
|
@ -141,7 +141,7 @@ pub enum Stmt {
|
|||
// --- Types and decls and garbage whatever
|
||||
//
|
||||
|
||||
#[derive(Debug, DebugPls)]
|
||||
#[derive(Debug, DebugPls, Clone, Copy)]
|
||||
pub enum IntTySignedness {
|
||||
Signed,
|
||||
Unsigned,
|
||||
|
|
@ -154,7 +154,7 @@ impl Default for IntTySignedness {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, DebugPls)]
|
||||
#[derive(Debug, DebugPls, Clone, Copy)]
|
||||
pub enum IntTyKind {
|
||||
Short,
|
||||
Int,
|
||||
|
|
@ -162,7 +162,7 @@ pub enum IntTyKind {
|
|||
LongLong,
|
||||
}
|
||||
|
||||
#[derive(Debug, DebugPls)]
|
||||
#[derive(Debug, DebugPls, Clone, Copy)]
|
||||
pub struct IntTy {
|
||||
pub sign: IntTySignedness,
|
||||
pub kind: IntTyKind,
|
||||
|
|
@ -283,4 +283,11 @@ impl DirectDeclarator {
|
|||
DirectDeclarator::WithParams { ident, params } => (ident, params),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn name(&self) -> Ident {
|
||||
match *self {
|
||||
DirectDeclarator::Ident(ident) => ident,
|
||||
DirectDeclarator::WithParams { ident, .. } => ident,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,14 +3,20 @@
|
|||
|
||||
use std::fmt::Debug;
|
||||
|
||||
use ast::TranslationUnit;
|
||||
|
||||
use self::parser::ParserError;
|
||||
use crate::token::Token;
|
||||
|
||||
pub mod ast;
|
||||
mod parser;
|
||||
mod pre;
|
||||
mod pretty;
|
||||
pub mod pretty;
|
||||
mod sym;
|
||||
mod token;
|
||||
|
||||
pub use sym::Symbol;
|
||||
|
||||
pub use crate::parser::Parser;
|
||||
|
||||
pub type Spanned<T> = (T, Span);
|
||||
|
|
@ -58,17 +64,7 @@ fn lex_and_pre(src: &str) -> impl Iterator<Item = (Token<'_>, Span)> + '_ {
|
|||
token::pre_tokens_to_tokens(pre_tokens)
|
||||
}
|
||||
|
||||
pub fn parse_file(src: &str) {
|
||||
pub fn parse_file(src: &str) -> Result<TranslationUnit, ParserError> {
|
||||
let lexer = lex_and_pre(src);
|
||||
let declarations = parser::parse_declarations(lexer);
|
||||
match declarations {
|
||||
Ok(declarations) => {
|
||||
dbg_pls::color!(&declarations);
|
||||
let mut printer = pretty::PrettyPrinter::new(std::io::stdout().lock(), false);
|
||||
println!("// START CODE -------------------");
|
||||
printer.translation_unit(&declarations).unwrap();
|
||||
println!("// END CODE -------------------");
|
||||
}
|
||||
Err(err) => eprintln!("error :(\n{:#?}", err),
|
||||
}
|
||||
parser::parse_declarations(lexer)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ use crate::{
|
|||
TranslationUnit, TypeSpecifier,
|
||||
},
|
||||
pre::Punctuator as P,
|
||||
sym::Symbol,
|
||||
token::{Keyword as Kw, Token as Tok},
|
||||
Span, Spanned,
|
||||
};
|
||||
|
|
@ -138,7 +139,7 @@ where
|
|||
|
||||
fn ident(&mut self) -> Result<Ident> {
|
||||
match self.next_t()? {
|
||||
(Tok::Ident(ident), span) => Ok((ident.to_string(), span)),
|
||||
(Tok::Ident(ident), span) => Ok((Symbol::intern(ident), span)),
|
||||
(tok, span) => Err(ParserError::new(
|
||||
span,
|
||||
format!("expected identifier, found `{tok}`"),
|
||||
|
|
@ -479,8 +480,14 @@ where
|
|||
}
|
||||
// all other stmts are indicated by keywords ...
|
||||
|
||||
if let (Tok::Kw(Kw::If), _) = self.peek_t()? {
|
||||
return self.if_statement();
|
||||
}
|
||||
|
||||
// it must be an expression stmt
|
||||
let (expr, span) = self.expr()?;
|
||||
expect!(self, Tok::Punct(P::Semicolon));
|
||||
|
||||
Ok((Stmt::Expr(expr), span))
|
||||
}
|
||||
|
||||
|
|
@ -547,6 +554,41 @@ where
|
|||
}
|
||||
Ok(decls)
|
||||
}
|
||||
|
||||
fn compount_or_single_statement(&mut self) -> Result<Vec<Spanned<Stmt>>> {
|
||||
if let Some((_, brace_span)) = eat!(self, Tok::Punct(P::BraceOpen)) {
|
||||
Ok(self.compound_statement(brace_span)?.0)
|
||||
} else {
|
||||
let stmt = self.statement()?;
|
||||
Ok(vec![stmt])
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Make sure this is correct.
|
||||
fn if_statement(&mut self) -> Result<Spanned<Stmt>> {
|
||||
let if_span = expect!(self, Tok::Kw(Kw::If));
|
||||
let _paren_span = expect!(self, Tok::Punct(P::ParenOpen));
|
||||
let cond = self.expr()?;
|
||||
let _paren_span = expect!(self, Tok::Punct(P::ParenClose));
|
||||
let then = self.compount_or_single_statement()?;
|
||||
let otherwise = if let Some(_) = eat!(self, Tok::Kw(Kw::Else)) {
|
||||
Some(self.compount_or_single_statement()?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let span = if_span
|
||||
.extend(cond.1)
|
||||
.extend_option(then.last().map(|s| s.1));
|
||||
Ok((
|
||||
Stmt::If {
|
||||
cond,
|
||||
then,
|
||||
otherwise,
|
||||
},
|
||||
span,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'src, I> Iterator for Parser<'src, I>
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ use crate::{
|
|||
},
|
||||
parser::{eat, expect, Parser, ParserError, Result},
|
||||
pre::Punctuator as P,
|
||||
sym::Symbol,
|
||||
token::{Constant, Token as Tok},
|
||||
Span, Spanned,
|
||||
};
|
||||
|
|
@ -23,7 +24,7 @@ where
|
|||
|
||||
fn get_lhs(&mut self) -> Result<Spanned<Expr>> {
|
||||
let (typ, span) = match self.peek_t()? {
|
||||
&(Tok::Ident(ident), span) => (Atom::Ident((ident.to_string(), span)), span),
|
||||
&(Tok::Ident(ident), span) => (Atom::Ident((Symbol::intern(ident), span)), span),
|
||||
&(Tok::StringLiteral(literal), span) => (Atom::String(literal.to_string()), span),
|
||||
&(Tok::Constant(Constant::Int(int)), span) => (Atom::Int(int), span),
|
||||
&(Tok::Constant(Constant::Float(float)), span) => (Atom::Float(float), span),
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: WithParams {
|
||||
ident: ("main", 5..9),
|
||||
ident: (main, 5..9),
|
||||
params: [],
|
||||
},
|
||||
pointer: false,
|
||||
|
|
@ -39,7 +39,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
(
|
||||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: Ident(("i", 22..23)),
|
||||
decl: Ident((i, 22..23)),
|
||||
pointer: false,
|
||||
},
|
||||
init: Some((Atom(Int(0)), 26..27)),
|
||||
|
|
@ -62,7 +62,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
(
|
||||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: Ident(("f", 39..40)),
|
||||
decl: Ident((f, 39..40)),
|
||||
pointer: false,
|
||||
},
|
||||
init: Some((
|
||||
|
|
|
|||
|
|
@ -0,0 +1,54 @@
|
|||
---
|
||||
source: parser/src/parser/tests.rs
|
||||
expression: "(parsed_pretty, pretty_printed_source)"
|
||||
---
|
||||
(
|
||||
Ok([
|
||||
(
|
||||
FunctionDef(FunctionDef {
|
||||
decl: Normal(NormalDecl {
|
||||
decl_spec: DeclSpec {
|
||||
ty: Integer(IntTy { sign: Signed, kind: Int }),
|
||||
attrs: "(empty)",
|
||||
},
|
||||
init_declarators: [
|
||||
(
|
||||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: WithParams {
|
||||
ident: (main, 5..9),
|
||||
params: [],
|
||||
},
|
||||
pointer: false,
|
||||
},
|
||||
init: None,
|
||||
},
|
||||
5..9,
|
||||
),
|
||||
],
|
||||
}),
|
||||
body: [
|
||||
(
|
||||
If {
|
||||
cond: (Atom(Int(1)), 22..23),
|
||||
then: [(Expr(Atom(String("a"))), 35..37)],
|
||||
otherwise: Some([
|
||||
(
|
||||
If {
|
||||
cond: (Atom(Int(2)), 55..56),
|
||||
then: [(Expr(Atom(String("b"))), 68..70)],
|
||||
otherwise: Some([(Expr(Atom(String("c"))), 94..96)]),
|
||||
},
|
||||
51..70,
|
||||
),
|
||||
]),
|
||||
},
|
||||
18..37,
|
||||
),
|
||||
],
|
||||
}),
|
||||
1..106,
|
||||
),
|
||||
]),
|
||||
"int main() {\n if (1) {\n \"a\"\n } else {\n if (2) {\n \"b\"\n } else {\n \"c\"\n }\n }\n}\n",
|
||||
)
|
||||
|
|
@ -16,7 +16,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: WithParams {
|
||||
ident: ("uwu", 5..8),
|
||||
ident: (uwu, 5..8),
|
||||
params: [
|
||||
FunctionParamDecl {
|
||||
decl_spec: (
|
||||
|
|
@ -28,7 +28,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
),
|
||||
declarator: (
|
||||
Declarator {
|
||||
decl: Ident(("owo", 14..17)),
|
||||
decl: Ident((owo, 14..17)),
|
||||
pointer: false,
|
||||
},
|
||||
14..17,
|
||||
|
|
@ -44,7 +44,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
),
|
||||
declarator: (
|
||||
Declarator {
|
||||
decl: Ident(("qwq", 23..26)),
|
||||
decl: Ident((qwq, 23..26)),
|
||||
pointer: false,
|
||||
},
|
||||
23..26,
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: WithParams {
|
||||
ident: ("uwu", 35..38),
|
||||
ident: (uwu, 35..38),
|
||||
params: [],
|
||||
},
|
||||
pointer: false,
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: WithParams {
|
||||
ident: ("uwu", 6..9),
|
||||
ident: (uwu, 6..9),
|
||||
params: [],
|
||||
},
|
||||
pointer: false,
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: WithParams {
|
||||
ident: ("main", 5..9),
|
||||
ident: (main, 5..9),
|
||||
params: [],
|
||||
},
|
||||
pointer: false,
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
(
|
||||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: Ident(("test", 5..9)),
|
||||
decl: Ident((test, 5..9)),
|
||||
pointer: false,
|
||||
},
|
||||
init: None,
|
||||
|
|
@ -38,7 +38,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
(
|
||||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: Ident(("uwu", 32..35)),
|
||||
decl: Ident((uwu, 32..35)),
|
||||
pointer: false,
|
||||
},
|
||||
init: None,
|
||||
|
|
@ -48,7 +48,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
(
|
||||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: Ident(("owo", 37..40)),
|
||||
decl: Ident((owo, 37..40)),
|
||||
pointer: false,
|
||||
},
|
||||
init: None,
|
||||
|
|
@ -72,7 +72,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: WithParams {
|
||||
ident: ("function", 68..76),
|
||||
ident: (function, 68..76),
|
||||
params: [],
|
||||
},
|
||||
pointer: false,
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: WithParams {
|
||||
ident: ("main", 5..9),
|
||||
ident: (main, 5..9),
|
||||
params: [],
|
||||
},
|
||||
pointer: false,
|
||||
|
|
@ -31,7 +31,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
(
|
||||
Expr(
|
||||
Postfix(ExprPostfix {
|
||||
lhs: (Atom(Ident(("puts", 18..22))), 18..22),
|
||||
lhs: (Atom(Ident((puts, 18..22))), 18..22),
|
||||
op: Call([(Atom(String("Hello, world!")), 23..37)]),
|
||||
}),
|
||||
),
|
||||
|
|
|
|||
|
|
@ -0,0 +1,45 @@
|
|||
---
|
||||
source: parser/src/parser/tests.rs
|
||||
expression: "(parsed_pretty, pretty_printed_source)"
|
||||
---
|
||||
(
|
||||
Ok([
|
||||
(
|
||||
FunctionDef(FunctionDef {
|
||||
decl: Normal(NormalDecl {
|
||||
decl_spec: DeclSpec {
|
||||
ty: Integer(IntTy { sign: Signed, kind: Int }),
|
||||
attrs: "(empty)",
|
||||
},
|
||||
init_declarators: [
|
||||
(
|
||||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: WithParams {
|
||||
ident: (main, 5..9),
|
||||
params: [],
|
||||
},
|
||||
pointer: false,
|
||||
},
|
||||
init: None,
|
||||
},
|
||||
5..9,
|
||||
),
|
||||
],
|
||||
}),
|
||||
body: [
|
||||
(
|
||||
If {
|
||||
cond: (Atom(Int(1)), 22..23),
|
||||
then: [(Expr(Atom(String("a"))), 35..37)],
|
||||
otherwise: Some([(Expr(Atom(String("b"))), 61..63)]),
|
||||
},
|
||||
18..37,
|
||||
),
|
||||
],
|
||||
}),
|
||||
1..73,
|
||||
),
|
||||
]),
|
||||
"int main() {\n if (1) {\n \"a\"\n } else {\n \"b\"\n }\n}\n",
|
||||
)
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
---
|
||||
source: parser/src/parser/tests.rs
|
||||
expression: "(parsed_pretty, pretty_printed_source)"
|
||||
---
|
||||
(
|
||||
Ok([
|
||||
(
|
||||
FunctionDef(FunctionDef {
|
||||
decl: Normal(NormalDecl {
|
||||
decl_spec: DeclSpec {
|
||||
ty: Integer(IntTy { sign: Signed, kind: Int }),
|
||||
attrs: "(empty)",
|
||||
},
|
||||
init_declarators: [
|
||||
(
|
||||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: WithParams {
|
||||
ident: (main, 5..9),
|
||||
params: [],
|
||||
},
|
||||
pointer: false,
|
||||
},
|
||||
init: None,
|
||||
},
|
||||
5..9,
|
||||
),
|
||||
],
|
||||
}),
|
||||
body: [
|
||||
(
|
||||
If {
|
||||
cond: (Atom(Int(1)), 22..23),
|
||||
then: [(Expr(Atom(String("a"))), 33..35)],
|
||||
otherwise: Some([(Expr(Atom(String("b"))), 55..57)]),
|
||||
},
|
||||
18..35,
|
||||
),
|
||||
],
|
||||
}),
|
||||
1..61,
|
||||
),
|
||||
]),
|
||||
"int main() {\n if (1) {\n \"a\"\n } else {\n \"b\"\n }\n}\n",
|
||||
)
|
||||
|
|
@ -15,7 +15,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
(
|
||||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: Ident(("a", 24..25)),
|
||||
decl: Ident((a, 24..25)),
|
||||
pointer: false,
|
||||
},
|
||||
init: Some((Atom(Int(1)), 28..29)),
|
||||
|
|
@ -38,7 +38,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
(
|
||||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: Ident(("b", 54..55)),
|
||||
decl: Ident((b, 54..55)),
|
||||
pointer: false,
|
||||
},
|
||||
init: Some((Atom(Int(2)), 58..59)),
|
||||
|
|
@ -61,7 +61,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
(
|
||||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: Ident(("c", 84..85)),
|
||||
decl: Ident((c, 84..85)),
|
||||
pointer: false,
|
||||
},
|
||||
init: Some((Atom(Int(3)), 88..89)),
|
||||
|
|
@ -84,7 +84,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
(
|
||||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: Ident(("d", 115..116)),
|
||||
decl: Ident((d, 115..116)),
|
||||
pointer: false,
|
||||
},
|
||||
init: Some((Atom(Int(4)), 119..120)),
|
||||
|
|
@ -107,7 +107,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
(
|
||||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: Ident(("e", 145..146)),
|
||||
decl: Ident((e, 145..146)),
|
||||
pointer: false,
|
||||
},
|
||||
init: Some((Atom(Int(6)), 149..150)),
|
||||
|
|
@ -130,7 +130,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
(
|
||||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: Ident(("f", 175..176)),
|
||||
decl: Ident((f, 175..176)),
|
||||
pointer: false,
|
||||
},
|
||||
init: Some((Atom(Int(5)), 179..180)),
|
||||
|
|
@ -153,7 +153,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
(
|
||||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: Ident(("g", 205..206)),
|
||||
decl: Ident((g, 205..206)),
|
||||
pointer: false,
|
||||
},
|
||||
init: Some((Atom(Int(7)), 209..210)),
|
||||
|
|
@ -179,7 +179,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
(
|
||||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: Ident(("h", 235..236)),
|
||||
decl: Ident((h, 235..236)),
|
||||
pointer: false,
|
||||
},
|
||||
init: Some((Atom(Int(8)), 239..240)),
|
||||
|
|
@ -205,7 +205,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
(
|
||||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: Ident(("i", 265..266)),
|
||||
decl: Ident((i, 265..266)),
|
||||
pointer: false,
|
||||
},
|
||||
init: Some((Atom(Int(9)), 269..270)),
|
||||
|
|
@ -228,7 +228,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
(
|
||||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: Ident(("j", 296..297)),
|
||||
decl: Ident((j, 296..297)),
|
||||
pointer: false,
|
||||
},
|
||||
init: Some((Atom(Int(10)), 300..302)),
|
||||
|
|
@ -251,7 +251,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
(
|
||||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: Ident(("k", 327..328)),
|
||||
decl: Ident((k, 327..328)),
|
||||
pointer: false,
|
||||
},
|
||||
init: Some((Atom(Int(11)), 331..333)),
|
||||
|
|
@ -274,7 +274,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
(
|
||||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: Ident(("l", 358..359)),
|
||||
decl: Ident((l, 358..359)),
|
||||
pointer: false,
|
||||
},
|
||||
init: Some((Atom(Int(12)), 362..364)),
|
||||
|
|
@ -297,7 +297,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
(
|
||||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: Ident(("m", 389..390)),
|
||||
decl: Ident((m, 389..390)),
|
||||
pointer: false,
|
||||
},
|
||||
init: Some((Atom(Int(13)), 393..395)),
|
||||
|
|
@ -320,7 +320,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
(
|
||||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: Ident(("n", 420..421)),
|
||||
decl: Ident((n, 420..421)),
|
||||
pointer: false,
|
||||
},
|
||||
init: Some((Atom(Int(14)), 424..426)),
|
||||
|
|
@ -343,7 +343,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
(
|
||||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: Ident(("o", 452..453)),
|
||||
decl: Ident((o, 452..453)),
|
||||
pointer: false,
|
||||
},
|
||||
init: Some((Atom(Int(15)), 456..458)),
|
||||
|
|
@ -366,7 +366,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
(
|
||||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: Ident(("p", 483..484)),
|
||||
decl: Ident((p, 483..484)),
|
||||
pointer: false,
|
||||
},
|
||||
init: Some((Atom(Int(16)), 487..489)),
|
||||
|
|
@ -389,7 +389,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
(
|
||||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: Ident(("q", 514..515)),
|
||||
decl: Ident((q, 514..515)),
|
||||
pointer: false,
|
||||
},
|
||||
init: Some((Atom(Int(17)), 518..520)),
|
||||
|
|
@ -412,7 +412,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
(
|
||||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: Ident(("r", 545..546)),
|
||||
decl: Ident((r, 545..546)),
|
||||
pointer: false,
|
||||
},
|
||||
init: Some((Atom(Int(18)), 549..551)),
|
||||
|
|
@ -438,7 +438,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
(
|
||||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: Ident(("s", 576..577)),
|
||||
decl: Ident((s, 576..577)),
|
||||
pointer: false,
|
||||
},
|
||||
init: Some((Atom(Int(19)), 580..582)),
|
||||
|
|
@ -464,7 +464,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
(
|
||||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: Ident(("t", 607..608)),
|
||||
decl: Ident((t, 607..608)),
|
||||
pointer: false,
|
||||
},
|
||||
init: Some((Atom(Int(20)), 611..613)),
|
||||
|
|
@ -490,7 +490,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
(
|
||||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: Ident(("u", 639..640)),
|
||||
decl: Ident((u, 639..640)),
|
||||
pointer: false,
|
||||
},
|
||||
init: Some((Atom(Int(21)), 643..645)),
|
||||
|
|
@ -516,7 +516,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
(
|
||||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: Ident(("v", 670..671)),
|
||||
decl: Ident((v, 670..671)),
|
||||
pointer: false,
|
||||
},
|
||||
init: Some((Atom(Int(22)), 674..676)),
|
||||
|
|
@ -542,7 +542,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
(
|
||||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: Ident(("w", 701..702)),
|
||||
decl: Ident((w, 701..702)),
|
||||
pointer: false,
|
||||
},
|
||||
init: Some((Atom(Int(23)), 705..707)),
|
||||
|
|
@ -568,7 +568,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
(
|
||||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: Ident(("x", 732..733)),
|
||||
decl: Ident((x, 732..733)),
|
||||
pointer: false,
|
||||
},
|
||||
init: Some((Atom(Int(24)), 736..738)),
|
||||
|
|
@ -594,7 +594,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
(
|
||||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: Ident(("y", 763..764)),
|
||||
decl: Ident((y, 763..764)),
|
||||
pointer: false,
|
||||
},
|
||||
init: Some((Atom(Int(25)), 767..769)),
|
||||
|
|
@ -620,7 +620,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
(
|
||||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: Ident(("z", 794..795)),
|
||||
decl: Ident((z, 794..795)),
|
||||
pointer: false,
|
||||
},
|
||||
init: Some((Atom(Int(26)), 798..800)),
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
(
|
||||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: Ident(("x", 5..6)),
|
||||
decl: Ident((x, 5..6)),
|
||||
pointer: false,
|
||||
},
|
||||
init: Some((
|
||||
|
|
@ -45,7 +45,7 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
(
|
||||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: Ident(("y", 21..22)),
|
||||
decl: Ident((y, 21..22)),
|
||||
pointer: false,
|
||||
},
|
||||
init: Some((
|
||||
|
|
@ -82,12 +82,12 @@ expression: "(parsed_pretty, pretty_printed_source)"
|
|||
(
|
||||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: Ident(("z", 45..46)),
|
||||
decl: Ident((z, 45..46)),
|
||||
pointer: false,
|
||||
},
|
||||
init: Some((
|
||||
Binary(ExprBinary {
|
||||
lhs: (Atom(Ident(("array", 50..55))), 50..55),
|
||||
lhs: (Atom(Ident((array, 50..55))), 50..55),
|
||||
rhs: (Atom(Int(9)), 56..57),
|
||||
op: Index,
|
||||
}),
|
||||
|
|
|
|||
|
|
@ -156,3 +156,49 @@ int main() {
|
|||
"#
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn if_else() {
|
||||
parse_test!(
|
||||
r#"
|
||||
int main() {
|
||||
if (1) {
|
||||
"a";
|
||||
} else {
|
||||
"b";
|
||||
}
|
||||
}
|
||||
"#
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn if_else_braceless() {
|
||||
parse_test!(
|
||||
r#"
|
||||
int main() {
|
||||
if (1)
|
||||
"a";
|
||||
else
|
||||
"b";
|
||||
}
|
||||
"#
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn else_if() {
|
||||
parse_test!(
|
||||
r#"
|
||||
int main() {
|
||||
if (1) {
|
||||
"a";
|
||||
} else if (2) {
|
||||
"b";
|
||||
} else {
|
||||
"c";
|
||||
}
|
||||
}
|
||||
"#
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ use crate::{
|
|||
FunctionParamDecl, InitDecl, IntTyKind, IntTySignedness, NormalDecl, PostfixOp, Stmt,
|
||||
TypeSpecifier, UnaryOp,
|
||||
},
|
||||
sym::Symbol,
|
||||
Span, Spanned,
|
||||
};
|
||||
|
||||
|
|
@ -33,6 +34,10 @@ impl<W: Write> PrettyPrinter<W> {
|
|||
write!(self.output, "{}", str)
|
||||
}
|
||||
|
||||
fn sym(&mut self, sym: Symbol) -> Result {
|
||||
sym.as_str(|sym| self.string(sym))
|
||||
}
|
||||
|
||||
fn print_indent(&mut self) -> Result {
|
||||
self.string(&INDENT.repeat(self.indent))
|
||||
}
|
||||
|
|
@ -88,6 +93,7 @@ impl<W: Write> PrettyPrinter<W> {
|
|||
self.linebreak()?;
|
||||
}
|
||||
self.dedent();
|
||||
self.print_indent()?;
|
||||
self.string("}")?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -96,7 +102,7 @@ impl<W: Write> PrettyPrinter<W> {
|
|||
match stmt {
|
||||
Stmt::Decl(decl) => self.decl(decl, false),
|
||||
Stmt::Labeled { label, stmt } => {
|
||||
self.string(&label.0)?;
|
||||
self.sym(label.0)?;
|
||||
self.string(":")?;
|
||||
self.linebreak()?;
|
||||
self.print_indent()?;
|
||||
|
|
@ -105,7 +111,7 @@ impl<W: Write> PrettyPrinter<W> {
|
|||
}
|
||||
Stmt::Compound(body) => self.block(body),
|
||||
Stmt::If {
|
||||
cond,
|
||||
cond: (cond, _),
|
||||
then,
|
||||
otherwise,
|
||||
} => {
|
||||
|
|
@ -163,7 +169,7 @@ impl<W: Write> PrettyPrinter<W> {
|
|||
}
|
||||
Stmt::Goto((label, _)) => {
|
||||
self.string("goto ")?;
|
||||
self.string(label)?;
|
||||
self.sym(*label)?;
|
||||
Ok(())
|
||||
}
|
||||
Stmt::Continue => self.string("continue"),
|
||||
|
|
@ -276,9 +282,9 @@ impl<W: Write> PrettyPrinter<W> {
|
|||
|
||||
fn direct_declarator(&mut self, declarator: &DirectDeclarator) -> Result {
|
||||
match declarator {
|
||||
DirectDeclarator::Ident(ident) => self.string(&ident.0),
|
||||
DirectDeclarator::Ident(ident) => self.sym(ident.0),
|
||||
DirectDeclarator::WithParams { ident, params } => {
|
||||
self.string(&ident.0)?;
|
||||
self.sym(ident.0)?;
|
||||
self.string("(")?;
|
||||
self.function_param_decls(params)?;
|
||||
self.string(")")?;
|
||||
|
|
@ -304,7 +310,7 @@ impl<W: Write> PrettyPrinter<W> {
|
|||
fn expr(&mut self, expr: &Expr) -> Result {
|
||||
match expr {
|
||||
Expr::Atom(atom) => match atom {
|
||||
Atom::Ident((ident, _)) => self.string(ident),
|
||||
Atom::Ident((ident, _)) => self.sym(*ident),
|
||||
Atom::Int(int) => write!(self.output, "{}", int),
|
||||
Atom::Float(float) => write!(self.output, "{}", float),
|
||||
Atom::String(string) => {
|
||||
|
|
@ -353,12 +359,12 @@ impl<W: Write> PrettyPrinter<W> {
|
|||
}
|
||||
PostfixOp::Member((ident, _)) => {
|
||||
self.string(".")?;
|
||||
self.string(ident)?;
|
||||
self.sym(*ident)?;
|
||||
Ok(())
|
||||
}
|
||||
PostfixOp::ArrowMember((ident, _)) => {
|
||||
self.string("->")?;
|
||||
self.string(ident)?;
|
||||
self.sym(*ident)?;
|
||||
Ok(())
|
||||
}
|
||||
PostfixOp::Increment => self.string("++"),
|
||||
|
|
|
|||
39
parser/src/sym.rs
Normal file
39
parser/src/sym.rs
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
use std::{cell::RefCell, fmt::Debug, marker::PhantomData};
|
||||
|
||||
use dbg_pls::DebugPls;
|
||||
use lasso::Spur;
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct Symbol {
|
||||
spur: Spur,
|
||||
not_send: PhantomData<*const ()>,
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
static INTERNER: RefCell<lasso::Rodeo> = RefCell::new(lasso::Rodeo::new());
|
||||
}
|
||||
|
||||
impl Symbol {
|
||||
pub fn intern(s: &str) -> Self {
|
||||
INTERNER.with(|i| Symbol {
|
||||
spur: i.borrow_mut().get_or_intern(s),
|
||||
not_send: PhantomData,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn as_str<R>(&self, f: impl FnOnce(&str) -> R) -> R {
|
||||
INTERNER.with(|i| f(i.borrow_mut().resolve(&self.spur)))
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for Symbol {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
INTERNER.with(|i| f.write_str(i.borrow_mut().resolve(&self.spur)))
|
||||
}
|
||||
}
|
||||
|
||||
impl DebugPls for Symbol {
|
||||
fn fmt(&self, f: dbg_pls::Formatter<'_>) {
|
||||
self.as_str(|s| f.debug_ident(s))
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue