add boolean support

This commit is contained in:
Mylloon 2022-12-08 21:30:39 +01:00
parent ab6e8c466b
commit 6f4eb269de
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
5 changed files with 23 additions and 4 deletions

14
ast.ml
View file

@ -1,8 +1,13 @@
type type_t = Int_t type type_t =
| Int_t
| Bool_t
module Syntax = struct module Syntax = struct
type ident = string type ident = string
type value = Int of int
type value =
| Int of int
| Bool of bool
type expr = type expr =
| Val of | Val of
@ -31,7 +36,10 @@ end
module IR = struct module IR = struct
type ident = string type ident = string
type value = Int of int
type value =
| Int of int
| Bool of bool
type expr = type expr =
| Val of value | Val of value

View file

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

View file

@ -7,6 +7,7 @@
let alpha = ['a'-'z' 'A'-'Z'] let alpha = ['a'-'z' 'A'-'Z']
let num = ['0'-'9'] let num = ['0'-'9']
let bool = "true" | "false"
let ident = alpha (alpha | num | '-' | '_')* let ident = alpha (alpha | num | '-' | '_')*
rule token = parse rule token = parse
@ -15,6 +16,8 @@ rule token = parse
| '\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) }
| "int" { Ltype (Int_t) } | "int" { Ltype (Int_t) }
| "bool" { Ltype (Bool_t) }
| bool as b { Lbool (bool_of_string b) }
| '=' { Lassign } | '=' { Lassign }
| ';' { Lsc } | ';' { Lsc }
| ident as i { Lvar i } | ident as i { Lvar i }

View file

@ -4,6 +4,7 @@
%} %}
%token <int> Lint %token <int> Lint
%token <bool> Lbool
%token <Ast.type_t> Ltype %token <Ast.type_t> Ltype
@ -38,6 +39,9 @@ expr:
| n = Lint { | n = Lint {
Val { value = Int (n) ; pos = $startpos(n) } Val { value = Int (n) ; pos = $startpos(n) }
} }
| b = Lbool {
Val { value = Bool (b) ; pos = $startpos(b) }
}
| v = Lvar { | v = Lvar {
Var { name = v ; pos = $startpos(v) } Var { name = v ; pos = $startpos(v) }
} }

View file

@ -9,11 +9,12 @@ exception Error of string * Lexing.position
let errt expected given pos = let errt expected given pos =
let str_of_type_t = function let str_of_type_t = function
| Int_t -> "int" | Int_t -> "int"
| Bool_t -> "bool"
in in
raise raise
(Error (Error
( Printf.sprintf ( Printf.sprintf
"Expected %s but %s given." "Expected %s but given %s"
(str_of_type_t expected) (str_of_type_t expected)
(str_of_type_t given) (str_of_type_t given)
, pos )) , pos ))
@ -31,6 +32,7 @@ let warn msg (pos : Lexing.position) =
let analyze_value = function let analyze_value = function
| Syntax.Int n -> Int n, Int_t | Syntax.Int n -> Int n, Int_t
| Syntax.Bool b -> Bool b, Bool_t
;; ;;
let analyze_expr env ua x = function let analyze_expr env ua x = function
@ -68,6 +70,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
| Int n -> "Int " ^ string_of_int n | Int n -> "Int " ^ string_of_int n
| Bool b -> "Bool " ^ string_of_bool b
and fmt_e = function and fmt_e = function
| Val v -> "Val (" ^ fmt_v v ^ ")" | Val v -> "Val (" ^ fmt_v v ^ ")"
| Var v -> "Var \"" ^ v ^ "\"" | Var v -> "Var \"" ^ v ^ "\""