Tuesday, April 3, 2007

EOPL - 3.2 - Exercise 3.5

Exercise 3.5 asks you to add a print primitive to the language, which prints its argument and returns the value 1. The relevant changes are simple. In module Eopl_3 only the definition of type Primitive and function apply_primitive are different:

// in eopl_3.fs (module Eopl_3)
type Primitive = AddPrim | SubtractPrim | MultPrim | IncrPrim
| DecrPrim | PrintPrim

let apply_primitive prim args =
match (prim, args) with
(AddPrim, [a; b]) -> a + b
| (SubtractPrim, [a; b]) -> a - b
| (MultPrim, [a; b]) -> a * b
| (IncrPrim, [a]) -> a + 1
| (DecrPrim, [a]) -> a - 1
| (PrintPrim, [a]) -> (print_endline (string_of_int a); 1)
| _ -> failwith "Incorrect argument list for primitive"

In the lexer specification a token for the print primitive is added:

rule token = parse
| whitespace { token lexbuf }
| '%' [^ '\n']* { token lexbuf }
| newline { record_newline lexbuf; token lexbuf }
| "+" { PLUS }
| "-" { MINUS }
| "*" { MULT }
| "(" { LPAREN }
| ")" { RPAREN }
| "," { COMMA }
| "add1" { INCR }
| "sub1" { DECR }
| "print" { PRINT }
| id { ID(lexeme lexbuf) }
| ['-']?digit+ { LIT (Int32.Parse(lexeme lexbuf)) }
| eof { EOF }

And, in the parser, this token must be declared and dealt with:

%token PRINT INCR DECR LPAREN RPAREN PLUS MINUS MULT COMMA EOF

Prim: PLUS { AddPrim }
| MINUS { SubtractPrim }
| MULT { MultPrim }
| INCR { IncrPrim }
| DECR { DecrPrim }
| PRINT { PrintPrim }

The remaining parts of these three files are the same as the previous two posts.

No comments: