Skip to content

Commit 41a9ce5

Browse files
committed
Merge remote-tracking branch 'origin/codegen-bene' into codegen
2 parents f6f48ba + 0b3aec9 commit 41a9ce5

File tree

10 files changed

+78
-80
lines changed

10 files changed

+78
-80
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838

3939
- name: Run cargo test w/ -D warnings
4040
if: ${{ runner.os == 'Linux' }}
41-
run: cargo test -- --test-threads=1
41+
run: cargo test --no-fail-fast -- --test-threads=1
4242
- name: Run cargo doc
4343
if: ${{ runner.os == 'Linux' }}
4444
run: cargo doc --no-deps --document-private-items --all-features

lib/src/codegen/ir.rs

+12-16
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ impl DIR {
5252
.iter()
5353
.flat_map(|m| m.as_bytes(&mut self.constant_pool))
5454
.collect();
55-
println!("Constant pool: {:?}", self.constant_pool);
5655
// Constant Pool
5756
result.extend_from_slice(&self.constant_pool.count().to_be_bytes());
5857
result.append(&mut self.constant_pool.as_bytes());
@@ -348,7 +347,7 @@ impl LocalVarPool {
348347
.iter()
349348
.position(|n| n == name)
350349
.map(|i| i as u8)
351-
.expect(&*format!("Local var {:?} not found in {:?}", name, self.0))
350+
.unwrap_or_else(|| panic!("Local var {:?} not found in {:?}", name, self.0))
352351
}
353352
}
354353
#[derive(Debug)]
@@ -455,9 +454,7 @@ pub struct NameAndType {
455454
}
456455

457456
fn get_instruction_length(istr: &Instruction) -> u16 {
458-
match istr {
459-
i => i.as_bytes().len() as u16,
460-
}
457+
istr.as_bytes().len() as u16
461458
}
462459

463460
fn get_instructions_length(instructions: &[Instruction]) -> u16 {
@@ -751,7 +748,7 @@ fn generate_code_stmt(
751748
));
752749
}
753750
Stmt::LocalVarDecl(types, name) => {
754-
local_var_pool.add(name.clone());
751+
local_var_pool.add(name);
755752
stack.inc(1);
756753
}
757754
Stmt::If(expr, stmt1, stmt2) => {
@@ -902,8 +899,7 @@ fn generate_code_stmt_expr(
902899
}
903900
StmtExpr::New(types, exprs) => {
904901
// Generate bytecode for new
905-
let class_index =
906-
constant_pool.add(Constant::Class(types.to_ir_string().to_string()));
902+
let class_index = constant_pool.add(Constant::Class(types.to_ir_string()));
907903
let method_index = constant_pool.add(Constant::MethodRef(MethodRef {
908904
class: types.to_ir_string(),
909905
method: NameAndType {
@@ -938,7 +934,7 @@ fn generate_code_stmt_expr(
938934
})
939935
.collect(),
940936
);
941-
fn generate_name_and_type(return_type: &Type, args: &Vec<Expr>) -> String {
937+
fn generate_name_and_type(return_type: &Type, args: &[Expr]) -> String {
942938
// Argument types comma seperated
943939
let argument_types = args
944940
.iter()
@@ -1008,7 +1004,7 @@ fn generate_code_expr(
10081004
stack.inc(1);
10091005
}
10101006
Expr::String(s) => {
1011-
let index = constant_pool.add(Constant::String(s.to_string()));
1007+
let index = constant_pool.add(Constant::String(s));
10121008
result.push(Instruction::ldc(index as u8));
10131009
stack.inc(1);
10141010
}
@@ -1060,7 +1056,7 @@ fn generate_code_expr(
10601056
let field_index = constant_pool.add(Constant::FieldRef(FieldRef {
10611057
class: class_name.to_string(),
10621058
field: NameAndType {
1063-
name: name.clone(),
1059+
name,
10641060
r#type: r#type.to_ir_string(),
10651061
},
10661062
}));
@@ -1386,16 +1382,16 @@ fn generate_code_expr(
13861382
let index = local_var_pool.get_index(&name);
13871383
match r#type {
13881384
Type::Int => {
1389-
result.push(Instruction::iload(index as u8));
1385+
result.push(Instruction::iload(index));
13901386
}
13911387
Type::Bool => {
1392-
result.push(Instruction::iload(index as u8));
1388+
result.push(Instruction::iload(index));
13931389
}
13941390
Type::Char => {
1395-
result.push(Instruction::iload(index as u8));
1391+
result.push(Instruction::iload(index));
13961392
}
13971393
Type::String => {
1398-
result.push(Instruction::aload(index as u8));
1394+
result.push(Instruction::aload(index));
13991395
}
14001396
_ => panic!("Unexpected type: {:?}", r#type),
14011397
}
@@ -1414,7 +1410,7 @@ fn generate_code_expr(
14141410
let index = constant_pool.add(Constant::FieldRef(FieldRef {
14151411
class: class_name.to_string(),
14161412
field: NameAndType {
1417-
name: name.clone(),
1413+
name,
14181414
r#type: r#type.to_ir_string(),
14191415
},
14201416
}));

lib/src/codegen/stack.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ impl StackMapTable {
394394
.to_be_bytes(),
395395
);
396396
// Attribute
397-
let mut attr: Vec<u8> = self.frames.iter().map(|x| x.as_bytes()).flatten().collect();
397+
let mut attr: Vec<u8> = self.frames.iter().flat_map(|x| x.as_bytes()).collect();
398398
// Attribute length
399399
// +2 because the 2 bytes for the entries length should also be included
400400
result.extend_from_slice(&(2 + attr.len() as u32).to_be_bytes());
@@ -405,7 +405,7 @@ impl StackMapTable {
405405
result
406406
}
407407
}
408-
408+
#[allow(clippy::upper_case_acronyms)]
409409
#[derive(Debug)]
410410
pub(crate) enum StackMapFrame {
411411
SAME(u8), // u8 in [0, 63]
@@ -447,7 +447,7 @@ impl StackMapFrame {
447447
let mut v = Vec::with_capacity(16);
448448
v.push(*appended_amount);
449449
v.extend_from_slice(&offset_delta.to_be_bytes());
450-
v.append(&mut types.iter().map(|t| t.as_bytes()).flatten().collect());
450+
v.append(&mut types.iter().flat_map(|t| t.as_bytes()).collect());
451451
let v = dbg!(v);
452452
v
453453
}
@@ -470,7 +470,7 @@ impl StackMapFrame {
470470
}
471471
}
472472
}
473-
473+
#[allow(clippy::upper_case_acronyms)]
474474
#[derive(Debug, Clone, PartialEq)]
475475
pub(crate) enum VerificationType {
476476
TOP,

lib/src/parser/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
#[allow(clippy::module_inception)]
12
mod parser;
23
pub use parser::*;

lib/src/parser/parser.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ use pest::error::Error;
1010
use pest::iterators::{Pair, Pairs};
1111
use pest::Parser;
1212
use pest_derive::Parser;
13+
use tracing::debug;
1314

1415
#[derive(Parser)]
1516
#[grammar = "../lib/src/parser/JavaGrammar.pest"]
1617
struct JavaParser;
17-
18+
#[allow(clippy::result_large_err)]
1819
pub fn parse_programm(file: &str) -> Result<Vec<Class>, Error<Rule>> {
1920
let prg: Pair<Rule> = JavaParser::parse(Rule::Program, file)?.next().unwrap();
2021

@@ -70,7 +71,7 @@ fn parse_method(pair: Pair<Rule>) -> MethodDecl {
7071
let method_name = next_id(&mut inners);
7172
let mut params = vec![];
7273
let mut body = None;
73-
while let Some(p) = inners.next() {
74+
for p in inners {
7475
match p.as_rule() {
7576
Rule::ParamDeclList => {
7677
let mut inner_param = p.into_inner();
@@ -112,7 +113,7 @@ fn parse_BlockStmt(pair: Pair<Rule>) -> Vec<Stmt> {
112113
pair.as_rule(),
113114
pair.as_str()
114115
);*/
115-
let rule = pair.as_rule().clone();
116+
let rule = pair.as_rule();
116117
let mut inner = pair.into_inner();
117118
match rule {
118119
Rule::BlockStmt => {
@@ -175,11 +176,11 @@ fn parse_BlockStmt(pair: Pair<Rule>) -> Vec<Stmt> {
175176
}
176177
}
177178
fn parse_Stmt(pair: Pair<Rule>) -> Vec<Stmt> {
178-
/* println!(
179+
debug!(
179180
"parse_Stmt: rule = {:?}, str = {}",
180181
pair.as_rule(),
181182
pair.as_str()
182-
);*/
183+
);
183184
match pair.as_rule() {
184185
Rule::Stmt => parse_Stmt(pair.into_inner().next().unwrap()), //@Notice this may be very wrong !!
185186
Rule::WhileStmt => {
@@ -246,12 +247,12 @@ fn parse_Stmt(pair: Pair<Rule>) -> Vec<Stmt> {
246247
}
247248

248249
fn parse_StmtExpr(pair: Pair<Rule>) -> StmtExpr {
249-
/*println!(
250+
debug!(
250251
"parse_StmtExpr: rule = {:?}, str = {}",
251252
pair.as_rule(),
252253
pair.as_str()
253-
);*/
254-
let rule = pair.as_rule().clone();
254+
);
255+
let rule = pair.as_rule();
255256
match rule {
256257
Rule::AssignExpr => {
257258
let mut inners = pair.into_inner();
@@ -339,7 +340,7 @@ fn parse_field_var_decl_list(jtype: Type, pair: Pair<Rule>) -> Vec<FieldDecl> {
339340
let mut inners = pair.into_inner();
340341
let mut var_decl = inners.next().unwrap().into_inner();
341342
let name = next_id(&mut var_decl);
342-
let val = var_decl.next().and_then(|expr| Some(parse_expr(expr)));
343+
let val = var_decl.next().map(parse_expr);
343344
let mut out = vec![FieldDecl {
344345
field_type: jtype.clone(),
345346
name,
@@ -348,7 +349,7 @@ fn parse_field_var_decl_list(jtype: Type, pair: Pair<Rule>) -> Vec<FieldDecl> {
348349
if let Some(p) = inners.next() {
349350
out.append(&mut parse_field_var_decl_list(jtype, p));
350351
}
351-
return out;
352+
out
352353
}
353354

354355
fn parse_Type(pair: Pair<Rule>) -> Type {
@@ -393,7 +394,7 @@ fn parse_expr(pair: Pair<Rule>) -> Expr {
393394
unreachable!()
394395
}
395396
};
396-
while let Some(p) = pairs.next() {
397+
for p in pairs {
397398
assert_eq!(p.as_rule(), Rule::Identifier);
398399
obj = Expr::InstVar(Box::new(obj), p.as_str().trim().to_string());
399400
}

lib/src/tests/tast_to_ast.rs

+7-11
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@ use super::*;
33
pub fn stmt_tast_to_ast(stmt: &Stmt) -> Stmt {
44
match stmt {
55
Stmt::TypedStmt(x, _typ) => stmt_tast_to_ast(x),
6-
Stmt::Block(stmts) => Block(stmts.iter().map(|x| stmt_tast_to_ast(x)).collect()),
6+
Stmt::Block(stmts) => Block(stmts.iter().map(stmt_tast_to_ast).collect()),
77
Stmt::Return(expr) => Return(expr_tast_to_ast(expr)),
88
Stmt::While(cond, body) => While(expr_tast_to_ast(cond), Box::new(stmt_tast_to_ast(body))),
99
Stmt::If(cond, body, elze) => If(
1010
expr_tast_to_ast(cond),
1111
Box::new(stmt_tast_to_ast(body)),
12-
match elze {
13-
Some(x) => Some(Box::new(stmt_tast_to_ast(x))),
14-
None => None,
15-
},
12+
elze.as_ref().map(|x| Box::new(stmt_tast_to_ast(x))),
1613
),
1714
Stmt::StmtExprStmt(stmt_expr) => StmtExprStmt(stmt_expr_tast_to_ast(stmt_expr)),
1815
_ => stmt.clone(),
@@ -22,14 +19,13 @@ pub fn stmt_tast_to_ast(stmt: &Stmt) -> Stmt {
2219
pub fn stmt_expr_tast_to_ast(stmt_expr: &StmtExpr) -> StmtExpr {
2320
match stmt_expr {
2421
StmtExpr::Assign(var, val) => Assign(expr_tast_to_ast(var), expr_tast_to_ast(val)),
25-
StmtExpr::New(typ, params) => New(
26-
typ.clone(),
27-
params.iter().map(|x| expr_tast_to_ast(x)).collect(),
28-
),
22+
StmtExpr::New(typ, params) => {
23+
New(typ.clone(), params.iter().map(expr_tast_to_ast).collect())
24+
}
2925
StmtExpr::MethodCall(obj, method, params) => MethodCall(
3026
expr_tast_to_ast(obj),
3127
method.clone(),
32-
params.iter().map(|x| expr_tast_to_ast(x)).collect(),
28+
params.iter().map(expr_tast_to_ast).collect(),
3329
),
3430
StmtExpr::TypedStmtExpr(x, _typ) => stmt_expr_tast_to_ast(x),
3531
}
@@ -61,7 +57,7 @@ pub fn tast_to_ast(class: &Class) -> Class {
6157
.map(|field| FieldDecl {
6258
field_type: field.field_type.clone(),
6359
name: field.name.clone(),
64-
val: field.val.clone().and_then(|x| Some(expr_tast_to_ast(&x))),
60+
val: field.val.clone().map(|x| expr_tast_to_ast(&x)),
6561
})
6662
.collect(),
6763
methods: class

lib/src/tests/to_java.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ pub fn class_to_java(class: &Class) -> String {
2222
let mut s: String = format!("class {} ", class.name);
2323
s += "{\n";
2424
for field in &class.fields {
25-
s = format!("{}{}", s, field_to_java(&field));
25+
s = format!("{}{}", s, field_to_java(field));
2626
}
2727
for method in &class.methods {
28-
s = format!("{}{}", s, method_to_java(&method));
28+
s = format!("{}{}", s, method_to_java(method));
2929
}
3030
s += "}\n";
3131
s
@@ -62,7 +62,7 @@ pub fn stmt_to_java(stmt: &Stmt, indent: u8) -> String {
6262
"{}{{\n{}\n{}}}",
6363
get_indents(indent - 1),
6464
stmts
65-
.into_iter()
65+
.iter()
6666
.map(|stmt| stmt_to_java(stmt, indent))
6767
.fold("".to_string(), |acc, s| acc + &s),
6868
get_indents(indent - 1)
@@ -97,8 +97,8 @@ pub fn stmt_to_java(stmt: &Stmt, indent: u8) -> String {
9797

9898
pub fn params_to_java(params: &Vec<Expr>) -> String {
9999
params
100-
.into_iter()
101-
.map(|expr| expr_to_java(expr))
100+
.iter()
101+
.map(expr_to_java)
102102
.reduce(|acc, s| format!("{}, {}", acc, s))
103103
.unwrap_or(String::new())
104104
}

lib/src/typechecker/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
#[allow(clippy::module_inception)]
12
pub mod typechecker;

0 commit comments

Comments
 (0)