add void type

This commit is contained in:
Mylloon 2022-12-09 22:20:05 +01:00
parent b1f1dc5aa8
commit 6cd78dabaf
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
5 changed files with 9 additions and 1 deletions

3
ast.ml
View file

@ -1,4 +1,5 @@
type type_t = type type_t =
| Void_t
| Int_t | Int_t
| Bool_t | Bool_t
| Func_t of type_t * type_t list | Func_t of type_t * type_t list
@ -7,6 +8,7 @@ module Syntax = struct
type ident = string type ident = string
type value = type value =
| Void
| Int of int | Int of int
| Bool of bool | Bool of bool
@ -48,6 +50,7 @@ module IR = struct
type ident = string type ident = string
type value = type value =
| Void
| Int of int | Int of int
| Bool of bool | Bool of bool

View file

@ -11,6 +11,7 @@ type info =
} }
let compile_value = function let compile_value = function
| Void -> [ Li (V0, 0) ]
| Int n -> [ Li (V0, n) ] | Int n -> [ Li (V0, n) ]
| Bool b -> [ Li (V0, if b then 1 else 0) ] | Bool b -> [ Li (V0, if b then 1 else 0) ]
;; ;;

View file

@ -15,6 +15,7 @@ let err msg pos =
let errt expected given pos = let errt expected given pos =
let rec string_of_type_t = function let rec string_of_type_t = function
| Void_t -> "void"
| Int_t -> "int" | Int_t -> "int"
| Bool_t -> "bool" | Bool_t -> "bool"
| Func_t (r, a) -> | Func_t (r, a) ->

View file

@ -14,10 +14,11 @@ rule token = parse
| [ ' ' '\t' ] { token lexbuf } | [ ' ' '\t' ] { token lexbuf }
| '\n' { Lexing.new_line lexbuf; token lexbuf } | '\n' { Lexing.new_line lexbuf; token lexbuf }
| num+ as n { Lint (int_of_string n) } | num+ as n { Lint (int_of_string n) }
| bool as b { Lbool (bool_of_string b) }
| "return" { Lreturn } | "return" { Lreturn }
| "int" { Ltype (Int_t) } | "int" { Ltype (Int_t) }
| "bool" { Ltype (Bool_t) } | "bool" { Ltype (Bool_t) }
| bool as b { Lbool (bool_of_string b) } | "void" { Ltype (Void_t) }
| '=' { Lassign } | '=' { Lassign }
| ';' { Lsc } | ';' { Lsc }
| '+' { Ladd } | '+' { Ladd }

View file

@ -4,6 +4,7 @@ open Baselib
open Errors open Errors
let analyze_value = function let analyze_value = function
| Syntax.Void -> Void, Void_t
| Syntax.Int n -> Int n, Int_t | Syntax.Int n -> Int n, Int_t
| Syntax.Bool b -> Bool b, Bool_t | Syntax.Bool b -> Bool b, Bool_t
;; ;;
@ -68,6 +69,7 @@ let analyze parsed = analyze_block _types_ [] parsed
let emit oc ast = let emit oc ast =
let rec fmt_v = function let rec fmt_v = function
| Void -> "Void"
| Int n -> "Int " ^ string_of_int n | Int n -> "Int " ^ string_of_int n
| Bool b -> "Bool " ^ string_of_bool b | Bool b -> "Bool " ^ string_of_bool b
and fmt_e = function and fmt_e = function