Skip to content

Commit

Permalink
Merge branch 'main' into restructure
Browse files Browse the repository at this point in the history
  • Loading branch information
Yey007 committed Apr 14, 2024
2 parents 2a55861 + 2ae6a41 commit 30291e4
Show file tree
Hide file tree
Showing 15 changed files with 169 additions and 40 deletions.
26 changes: 26 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
PY := $(shell which python3 || which python || which pypy3 || which pypy)

.PHONY: build
build:
@rm -f ./main
opam exec -- dune build
@cp _build/install/default/bin/x86ISTMB ./main
@chmod u+x ./main
@chmod u+x .githooks/pre-commit
@make README

.PHONY: README
README:
$(PY) readme.py

.PHONY: test
test: build
opam exec -- dune test

.PHONY: bisect
bisect:
@find . -name '*.coverage' | xargs rm -f
@OUNIT_CI=true dune test --instrument-with bisect_ppx --force
@bisect-ppx-report html
@if [ $$(command -v pup) ]; then \
printf "Overall Coverage: "; \
cat _coverage/index.html | pup html body div#header h2 text{}; \
fi

.PHONY: view
view:
open _coverage/index.html

# .PHONY: utop
# utop:
# echo "open X86ISTMB;;" | dune utop
Expand All @@ -29,3 +51,7 @@ serve: docs
@echo '==> Serving at localhost:$(PORT)'
@cd docs/html; $(shell which python || which python3) -m http.server $(PORT)

.PHONY: cloc
cloc:
@make build > /dev/null
@echo "$$(cloc bin lib --json | jq .SUM.code) lines of code"
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
# x86ISTMB
<!-- THIS FILE IS GENERATED AUTOMATICALLY. -->
<!-- DO NOT EDIT THIS FILE. -->
<!-- EDIT README.md.template INSTEAD. -->
# x86ISTMB v0.1.0


![CI Status](https://github.com/ethanuppal/cs3110_compiler/actions/workflows/ci.yaml/badge.svg)

```shell
> "x86 is simple trust me bro"
> Last updated: 2024-04-14 19:14:39.985480
```
$ ./main -h
CS 3110 final project
Expand All @@ -13,6 +20,8 @@ Usage: ./main [-h|-v]
-v,--version prints version info
-g,--gen only produces IR
-O,--optimize runs optimizations
```
```
$ ./main -v
x86ISTMB v0.1.0
Expand All @@ -21,8 +30,6 @@ Written by: Utku Melemeti, Jason Klein, Jeffrey Huang, Vijay Shanmugam, Ethan Up

## Group

This is the CS 3110 final project by:

- Jason Klein (jak532)
- Utku Melemetci (um44)
- Jeffrey Huang (jrh382)
Expand All @@ -32,7 +39,8 @@ This is the CS 3110 final project by:
## Usage

The `Makefile` in the project root directory enables easy usage.
Simply run `make` to build the project executable, e.g., `./main --help`.
Simply run `make` to build the project executable, `./main`.
`make clean` will clear all build and executable files.
Documentation is in [introduction.md](docs/introduction.md).
Installation instructions are in [INSTALL.md](INSTALL.md)
Installation instructions are in [INSTALL.md](INSTALL.md).
A full user manual is in [user_manual.md](docs/user_manual.md).
18 changes: 10 additions & 8 deletions README.md.template
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
# x86ISTMB
# x86ISTMB {{VERSION_NUM}}

```shell
![CI Status](https://github.com/ethanuppal/cs3110_compiler/actions/workflows/ci.yaml/badge.svg)

> "x86 is simple trust me bro"
> Last updated: {{DATE}}

```
$ ./main -h
{{HELP}}
``````shell
$ ./main -v
{{VERSION}}
{{HELP}}```
```
$ ./main -v
{{VERSION}}```

## Group

This is the CS 3110 final project by:

- Jason Klein (jak532)
- Utku Melemetci (um44)
- Jeffrey Huang (jrh382)
Expand Down
11 changes: 8 additions & 3 deletions docs/user_manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ make test
Running `make build` will produce an executable in the same directory called `./main`.
You can see what options are available with `./main -h` or `./main --help`, and view versioning information with `./main -v` or `./main --version`.

To clean up all build and executable files, run `make clean`.
Here are some other commands.

To generate documentation, run `make docs`.
Then, view it locally with `make serve PORT=8003`, which will host the documentation website at `localhost:8003`.
| Command | Description |
|------------|------------------------|
| `make clean` | Removes all build and executable files |
| `make docs` | Generates documentation |
| `make serve PORT=8003` | Hosts the documentation website at `localhost:8003` |
| `make bisect` | Runs `make test` and outputs the percentage of coverage |
| `make cloc` | Counts the lines of code in the project.
4 changes: 3 additions & 1 deletion lib/dune
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
(name x86ISTMB)
(libraries batteries menhirLib)
(preprocess
(pps ppx_inline_test)))
(pps ppx_inline_test))
(instrumentation
(backend bisect_ppx)))

(rule
(targets project_root.ml)
Expand Down
5 changes: 3 additions & 2 deletions lib/frontend/analysis.ml
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ let rec infer_expr ctx hint expr =
| FunctionExpr _ -> ()

(** @raise TypeInferenceError on failure. *)
let infer_stmt ctx stmt =
let rec infer_stmt ctx stmt =
match stmt with
| Declaration { name; hint; expr } ->
(infer_expr ctx hint expr;
Expand All @@ -131,9 +131,10 @@ let infer_stmt ctx stmt =
}));
Context.insert ctx name (type_of_expr expr |> Option.get)
| Print expr -> infer_expr ctx None expr
| Function { name = _; body } -> infer body
| _ -> ()

let infer prog =
and infer prog =
let ctx = Context.make () in
Context.push ctx;
List.iter (infer_stmt ctx) prog;
Expand Down
65 changes: 52 additions & 13 deletions lib/frontend/ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,56 @@ let type_of_expr = function
| Prefix { op = _; rhs = _; ty } -> ty
| FunctionExpr _ -> None

(** TODO: to string functions *)
let pp_op fmt op =
let char =
match op with
| Plus -> "+"
| Minus -> "-"
| Times -> "*"
| Divide -> "/"
| Mod -> "%"
let op_to_string op =
match op with
| Plus -> "+"
| Minus -> "-"
| Times -> "*"
| Divide -> "/"
| Mod -> "%"

let rec expr_to_string = function
| Var { name; ty = _ } -> name
| ConstInt n -> string_of_int n
| ConstBool b -> string_of_bool b
| Infix { lhs; op; rhs; ty = _ } ->
"(" ^ expr_to_string lhs ^ " " ^ op_to_string op ^ " "
^ expr_to_string rhs ^ ")"
| Prefix { op; rhs; ty = _ } ->
"(" ^ op_to_string op ^ expr_to_string rhs ^ ")"
| FunctionExpr _ -> "<func>"

let stmt_to_string =
let add_indent = String.make 4 ' ' in
let rec stmt_to_string_aux indent stmt =
let make_string = function
| Call name -> name ^ "()"
| Declaration { name; hint; expr } ->
let expr_type = type_of_expr expr in
let display_type = if expr_type = None then hint else expr_type in
let hint_str =
match display_type with
| Some t -> ": " ^ Type.to_string t
| None -> ""
in
"let " ^ name ^ hint_str ^ " = " ^ expr_to_string expr
| Assignment (name, expr) -> name ^ " = " ^ expr_to_string expr
| Function { name; body } ->
"func " ^ name ^ "() {\n"
^ (body
|> List.map (stmt_to_string_aux (indent ^ add_indent))
|> String.concat "")
^ "}"
| Print expr -> "print " ^ expr_to_string expr
in
indent ^ make_string stmt ^ "\n"
in
Format.pp_print_string fmt char
stmt_to_string_aux ""

(** TODO: to string functions *)
let pp_op fmt =
let open Util in
op_to_string >> Format.pp_print_string fmt

let rec pp_expr fmt = function
| Var { name; _ } -> Format.pp_print_string fmt name
Expand All @@ -88,7 +127,6 @@ let rec pp_expr fmt = function
| Prefix { op; rhs; _ } ->
Format.pp_print_string fmt "(";
pp_op fmt op;
Format.pp_print_string fmt " ";
pp_expr fmt rhs;
Format.pp_print_string fmt ")"
| FunctionExpr _ -> Format.pp_print_string fmt "<func>"
Expand All @@ -107,10 +145,11 @@ let rec pp_stmt fmt = function
Format.fprintf fmt "%s = " name;
pp_expr fmt expr
| Function { name; body } ->
Format.fprintf fmt "func %s () {" name;
Format.fprintf fmt "func %s() {" name;
(* Go down a line and indent by two *)
Format.pp_print_break fmt 0 2;
Format.pp_open_vbox fmt 0;
Format.pp_force_newline fmt ();
Format.pp_open_hvbox fmt 0;
Format.pp_print_list pp_stmt fmt body;
Format.pp_close_box fmt ();
Format.pp_print_cut fmt ();
Expand Down
27 changes: 27 additions & 0 deletions readme.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
if __name__ == "__main__":
import subprocess
from datetime import datetime as dt

# requires : ./main has been built
with open("README.md.template", "r") as f:
readme = {"content": f.read()}

def put(var, text):
readme["content"] = readme["content"].replace("{{" + var + "}}", text)

put("HELP", subprocess.check_output(["./main", "-h"], text=True))
put("VERSION", subprocess.check_output(["./main", "-v"], text=True))
put("DATE", str(dt.now()))
put(
"VERSION_NUM",
subprocess.check_output(
"opam exec -- ocaml -e '#use \"./lib/meta.ml\";; print_endline (Version.to_string get.version)'",
shell=True,
text=True,
),
)
with open("README.md", "w") as f:
f.write(
"<!-- THIS FILE IS GENERATED AUTOMATICALLY. -->\n<!-- DO NOT EDIT THIS FILE. -->\n<!-- EDIT README.md.template INSTEAD. -->\n"
+ readme["content"]
)
5 changes: 5 additions & 0 deletions test/snapshots/v0.1.0/type5.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let x = true
func foo() {
let x = 4
print +x
}
5 changes: 5 additions & 0 deletions test/snapshots/v0.1.0/type5.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let x: Bool = true
func foo() {
let x: Int = 4
print (+x)
}
5 changes: 5 additions & 0 deletions test/snapshots/v0.1.0/type6.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let x = 1
func foo() {
let x = 1
}
print x
5 changes: 5 additions & 0 deletions test/snapshots/v0.1.0/type6.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let x: Int = 1
func foo() {
let x: Int = 1
}
print x
4 changes: 4 additions & 0 deletions test/snapshots/v0.1.0/type7.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
func foo() {
let x = 1
}
print x
1 change: 1 addition & 0 deletions test/snapshots/v0.1.0/type7.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Type resolution error (symbol='x')
8 changes: 1 addition & 7 deletions test/test_x86ISTMB.ml
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,11 @@ let snapshot_test_suite =
let () = ignore interpreter_transform in
let transform path input =
let open X86ISTMB in
let open Util in
let stmt_to_string stmt =
Ast.pp_stmt Format.str_formatter stmt;
Format.flush_str_formatter ()
in
if String.starts_with ~prefix:"type" path then
try
let statements = Parse_lex.lex_and_parse input in
Analysis.infer statements;
List.map (stmt_to_string >> fun s -> s ^ "\n") statements
|> String.concat ""
statements |> List.map Ast.stmt_to_string |> String.concat ""
with
| Analysis.TypeInferenceError err ->
Printexc.to_string (Analysis.TypeInferenceError err) ^ "\n"
Expand Down

0 comments on commit 30291e4

Please sign in to comment.