Skip to content

Commit 774e0f8

Browse files
Flippchenmfloto
andcommitted
fix: compile
Co-authored-by: mfloto <[email protected]>
1 parent e5e5a8a commit 774e0f8

File tree

2 files changed

+67
-4
lines changed

2 files changed

+67
-4
lines changed

lib/src/typechecker/typechecker.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -263,15 +263,27 @@ impl TypeChecker {
263263
fn type_stmt_expr(&self, stmt_expr: &StmtExpr) -> StmtExpr {
264264
match stmt_expr {
265265
StmtExpr::Assign(name, expr) => StmtExpr::TypedStmtExpr(
266-
Box::new(StmtExpr::Assign(
267-
name.clone(),
268-
Box::new(self.type_expr(expr)),
269-
)),
266+
Box::new(StmtExpr::Assign(name.clone(), self.type_expr(expr))),
270267
Type::Int,
271268
),
272269
StmtExpr::TypedStmtExpr(stmt_expr, t) => {
273270
StmtExpr::TypedStmtExpr(Box::new(self.type_stmt_expr(stmt_expr)), t.clone())
274271
}
272+
StmtExpr::New(t, exprs) => StmtExpr::TypedStmtExpr(
273+
Box::new(StmtExpr::New(
274+
t.clone(),
275+
exprs.iter().map(|e| self.type_expr(e)).collect(),
276+
)),
277+
Type::Int,
278+
),
279+
StmtExpr::MethodCall(expr, name, exprs) => StmtExpr::TypedStmtExpr(
280+
Box::new(StmtExpr::MethodCall(
281+
self.type_expr(expr),
282+
name.clone(),
283+
exprs.iter().map(|e| self.type_expr(e)).collect(),
284+
)),
285+
Type::Int,
286+
),
275287
}
276288
}
277289
fn infer_expr_type(&self, expr: &Expr) -> Result<Type, String> {

src/main.rs

+51
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,60 @@
1+
use lib::typechecker::typechecker;
2+
use lib::types::{Class, Expr, FieldDecl, MethodDecl, Prg, Stmt, Type};
3+
use serde_json::Value;
4+
use std::fs::File;
5+
use std::io::Read;
16
use tracing::info;
27

38
fn main() -> color_eyre::Result<()> {
49
color_eyre::install()?;
510
tracing_subscriber::fmt::init();
611
info!("Hello RustyJ!");
712
lib::hi();
13+
//let mut file = File::open("tests/If-AST.json")?;
14+
//let mut ast_string = String::new();
15+
16+
//file.read_to_string(&mut ast_string)?;
17+
//let ast_value: Value = serde_json::from_str(&ast_string)?;
18+
//println!("{:#?}", ast_value);
19+
//let ast: Class = serde_json::from_value(ast_value.clone())?;
20+
//println!("{:#?}", ast);
21+
let class: Class = Class {
22+
name: "test".to_string(),
23+
fields: vec![],
24+
methods: vec![MethodDecl {
25+
name: "f".to_string(),
26+
params: vec![(Type::Char, "c".to_string())],
27+
retType: Type::Bool,
28+
body: Stmt::Block(vec![
29+
Stmt::If(
30+
Expr::Binary(
31+
"==".to_string(),
32+
Box::new(Expr::LocalOrFieldVar("c".to_string())),
33+
Box::new(Expr::Char('a')),
34+
),
35+
Box::new(Stmt::Return(Expr::Bool(true))),
36+
None,
37+
),
38+
Stmt::Return(Expr::Bool(false)),
39+
]),
40+
}],
41+
};
42+
43+
let program: Prg = vec![class.clone()];
44+
let mut typechecker = typechecker::TypeChecker::new(program);
45+
typechecker
46+
.unwrap()
47+
.check_program()
48+
.expect("TODO: panic message");
49+
50+
// Create a new json file
51+
let mut file = File::create("typed_if-test-local.json")?;
52+
53+
serde_json::to_writer_pretty(&mut file, &class)?;
54+
55+
//let typed_ast_string = serde_json::to_string_pretty(&ast)?;
56+
57+
//let mut file = File::create("typed_if-ast.txt")?;
58+
//file.write_all(typed_ast_string.as_bytes())?;
859
Ok(())
960
}

0 commit comments

Comments
 (0)