From 6cd78dabafcd9ad51d93a2e176dc18789198e7bf Mon Sep 17 00:00:00 2001 From: Mylloon Date: Fri, 9 Dec 2022 22:20:05 +0100 Subject: [PATCH] add void type --- ast.ml | 3 +++ compiler.ml | 1 + errors.ml | 1 + lexer.mll | 3 ++- semantics.ml | 2 ++ 5 files changed, 9 insertions(+), 1 deletion(-) diff --git a/ast.ml b/ast.ml index c0957b0..20f2eb0 100644 --- a/ast.ml +++ b/ast.ml @@ -1,4 +1,5 @@ type type_t = + | Void_t | Int_t | Bool_t | Func_t of type_t * type_t list @@ -7,6 +8,7 @@ module Syntax = struct type ident = string type value = + | Void | Int of int | Bool of bool @@ -48,6 +50,7 @@ module IR = struct type ident = string type value = + | Void | Int of int | Bool of bool diff --git a/compiler.ml b/compiler.ml index f0fdc30..dd8a355 100644 --- a/compiler.ml +++ b/compiler.ml @@ -11,6 +11,7 @@ type info = } let compile_value = function + | Void -> [ Li (V0, 0) ] | Int n -> [ Li (V0, n) ] | Bool b -> [ Li (V0, if b then 1 else 0) ] ;; diff --git a/errors.ml b/errors.ml index ac8923e..fa3ff31 100644 --- a/errors.ml +++ b/errors.ml @@ -15,6 +15,7 @@ let err msg pos = let errt expected given pos = let rec string_of_type_t = function + | Void_t -> "void" | Int_t -> "int" | Bool_t -> "bool" | Func_t (r, a) -> diff --git a/lexer.mll b/lexer.mll index b8bbdc7..c6a34d5 100644 --- a/lexer.mll +++ b/lexer.mll @@ -14,10 +14,11 @@ rule token = parse | [ ' ' '\t' ] { token lexbuf } | '\n' { Lexing.new_line lexbuf; token lexbuf } | num+ as n { Lint (int_of_string n) } +| bool as b { Lbool (bool_of_string b) } | "return" { Lreturn } | "int" { Ltype (Int_t) } | "bool" { Ltype (Bool_t) } -| bool as b { Lbool (bool_of_string b) } +| "void" { Ltype (Void_t) } | '=' { Lassign } | ';' { Lsc } | '+' { Ladd } diff --git a/semantics.ml b/semantics.ml index d9d87e6..e627958 100644 --- a/semantics.ml +++ b/semantics.ml @@ -4,6 +4,7 @@ open Baselib open Errors let analyze_value = function + | Syntax.Void -> Void, Void_t | Syntax.Int n -> Int n, Int_t | Syntax.Bool b -> Bool b, Bool_t ;; @@ -68,6 +69,7 @@ let analyze parsed = analyze_block _types_ [] parsed let emit oc ast = let rec fmt_v = function + | Void -> "Void" | Int n -> "Int " ^ string_of_int n | Bool b -> "Bool " ^ string_of_bool b and fmt_e = function