Skip to content

Commit

Permalink
Now handles octal and hexadecimal integer litterals, and uses int32s.
Browse files Browse the repository at this point in the history
  • Loading branch information
Berke Durak committed Dec 19, 2007
1 parent 72e271c commit 690fa7e
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
6 changes: 3 additions & 3 deletions ast.ml
Expand Up @@ -60,7 +60,7 @@ and expr =
and property_name =
| PN_String of string
| PN_Float of float
| PN_Int of int
| PN_Int of int32
| PN_Empty
and binop =
| B_mul
Expand Down Expand Up @@ -115,7 +115,7 @@ and assignment_operator =
| A_or
and litteral =
| Float of float
| Int of int
| Int of int32
| String of string
| Regexp of string * string
| Bool of bool
Expand Down Expand Up @@ -199,7 +199,7 @@ and iter_over_expr_in_lhs info f = function
let scribe_property_name cd oc = function
| PN_String u -> cd.cd_print oc "%S" u
| PN_Float f -> cd.cd_print oc "%f" f
| PN_Int x -> cd.cd_print oc "%d" x
| PN_Int x -> cd.cd_print oc "%ld" x
| PN_Empty -> cd.cd_print oc "*empty*"
;;
(* ***)
6 changes: 4 additions & 2 deletions convert.ml
Expand Up @@ -8,6 +8,8 @@ open Ecma;;
exception Bad_tree of Ecma.tree;;
exception Bad_trees of Ecma.tree list;;

let int32_of_string u = Int64.to_int32 (Int64.of_string u);;

let rec convert = function
| Node(N_Root, _, [Node(N_Program, _, sl)]) -> convert_source_elements sl
| t -> raise (Bad_tree t)
Expand Down Expand Up @@ -120,7 +122,7 @@ and convert_expression = function
| _ -> raise (Bad_tree t)
in
L(Regexp(body, options))
| Node(N_Integer, [A_value, n], []) -> L(Int(int_of_string n))
| Node(N_Integer, [A_value, n], []) -> L(Int(int32_of_string n))
| Node(N_Float, [A_value, f], []) -> L(Float(float_of_string f))
| Node(N_Shift, [], [x1;Node(N_Asr, _, _);x2]) -> B(B_asr, convert_expression x1, convert_expression x2)
| Node(N_Shift, [], [x1;Node(N_Lsr, _, _);x2]) -> B(B_lsr, convert_expression x1, convert_expression x2)
Expand Down Expand Up @@ -193,7 +195,7 @@ and convert_expression = function
| t -> raise (Bad_tree t)
and convert_property_name = function
| Node(N_Ident, [A_name, name], []) | Node(N_String, [A_value, name], []) -> PN_String name
| Node(N_Integer, [A_value, value], []) -> PN_Int(int_of_string value)
| Node(N_Integer, [A_value, value], []) -> PN_Int(int32_of_string value)
| Node(N_Float, [A_value, value], []) -> PN_Float(float_of_string value)
| t -> raise (Bad_tree t)
and convert_property = function
Expand Down
8 changes: 7 additions & 1 deletion ecma.peg
Expand Up @@ -7,7 +7,13 @@ space ::=
| "/*" ((~ "*/") sigma)* "*/"
;

integer ::= value:('0' | [1-9][0-9]*);
integer ::=
value:(
"0x" [0-9a-fA-F]+
| '0' [0-7]+
| [1-9][0-9]*
| '0')
;
float ::= value:(('0' | [1-9][0-9]* )? '.' [0-9]+);

alphanum ::= [A-Za-z_0-9$]; (* Check lexical rules for identifiers *)
Expand Down
2 changes: 1 addition & 1 deletion eval.ml
Expand Up @@ -13,7 +13,7 @@ module PNM = Map.Make(struct type t = property_name let compare = compare end);;
type 'func t =
| T_String of string option
| T_Float of float option
| T_Int of int option
| T_Int of int32 option
| T_Regexp
| T_Bool of bool option
| T_Null
Expand Down
4 changes: 2 additions & 2 deletions generate.ml
Expand Up @@ -227,7 +227,7 @@ let generate f pg =
fp f "@\n";
begin
match pn with
| PN_Int x -> fp f "%d" x
| PN_Int x -> fp f "%ld" x
| PN_Float x -> fp f "%f" x
| PN_String x when is_property_safe x -> fp f "%s" x
| PN_String x -> fp f "%S" x
Expand Down Expand Up @@ -314,7 +314,7 @@ let generate f pg =
block sl
and litteral = function
| Float x -> fp f "%f" x
| Int x -> fp f "%d" x
| Int x -> fp f "%ld" x
| String s -> fp f "%S" s
| Regexp(r, o) -> fp f "/%s/%s" r o
| Bool b -> fp f "%b" b
Expand Down

0 comments on commit 690fa7e

Please sign in to comment.