Fill This Form To Receive Instant Help
Homework answers / question archive / LAB 11 - ciLisp Final Project COMP-232 Programming Languages TASK 4 SUMMARY In this task, you will: Implement the rand, read, equal, less, greater and print functions
LAB 11 - ciLisp Final Project
COMP-232 Programming Languages
TASK 4
SUMMARY
In this task, you will:
FUNCTION SPECIFICATIONS
The functions will will work as follows:
CONDITIONAL SPECIFICATIONS
The conditional operator will require adding the following to the grammar:
s_expr ::= ( COND s_expr s_expr s_expr )
COND ::= "cond"
You'll also need to add several more functions to the FUNC tokenization definition, the FUNC_TYPE enum and funcNames array.
Conditionals can be implemented via adding a new AST_NODE_TYPE member and corresponding struct, and integrating this new type into the AST_NODE. Do not implmenet conditionals by adding a new function instead; syntax errors should be thrown if a conditional doesn't have exactly 3 s_exprs.
If the first of the three s_expr (the condition) is nonzero (i.e. "true") when evaluated, then the conditional returns the value of the second s_expr. Otherwise, the condition is zero (i.e. "false") so the third s_expr's value is returned.
If A, B and C are expressions, then CI Lisp's ( cond A B C ) is the equivalent of A ? B : C in C (the ternary operator).
SAMPLE RUN
The inputs for the sample run are provided in the task 4 sample input. It demonstrates the new functionality in very simple inputs, and tests some of the old functionality. As always, you should construct better test inputs to ensure that the new functionality is fully integrated with the old. A file is also provided for read inputs which matches those in this run; running with this file as the read_target will lead to the same outputs (but it won't be quite as pretty).
Make sure you understand why each value is printed in the sample run. Note that the result of the full program evaluation is always printed, so the last printed value following each input is not from a call to the print function, but from the printRetVal calls in the .y file, in the program productions. The last two inputs (not including the quit) in the sample run provide an opportunity to test specifications from task 2 which are often overlooked:
WARNING: print called with no operands!
Double : nan
> (print 1) Integer : 1
Integer : 1
> (print 1 2)
WARNING: print called with extra (ignored) operands!
Integer : 1
Integer : 1
> (add 1 (print 2) )
Integer : 2
Integer : 3
> (rand)
Double : 0.000008
> (rand)
Double : 0.131538
> (rand)
Double : 0.755605
> (rand)
Double : 0.458650
> (rand)
Double : 0.532767
> (rand)
Double : 0.218959
> (rand)
Double : 0.047045
> (rand)
Double : 0.678865
> (rand)
Double : 0.679296
> (rand)
Double : 0.934693
> (read) read :: 1 Integer : 1
> (read) read :: asdf
WARNING: Invalid read entry! NAN returned!
Double : nan
> (read) read :: .5
WARNING: Invalid read entry! NAN returned!
Double : nan
> (read) read :: -5.5
> (add (read) (read)) read :: 10 read :: -10 Integer : 0
> (equal 0 0)
Integer : 1
> (equal 0 0.0)
Integer : 1
> (equal 0 0.0001)
Integer : 0
> (less 0 0)
Integer : 0
> (less -1 0)
Integer : 1
> (less 0 -0.00001)
Integer : 0
> (greater 0 1)
Integer : 0
> (greater 1 0)
Integer : 1
> (greater 0 0.0)
Integer : 0
> ( ( let (x 0) (y 1) ) (less x y) )
Integer : 1
> (cond 0 5 6)
Integer : 6
> (cond 1 5 6)
Integer : 5
> (cond (less 0 1) 5 6)
Integer : 5
> (cond (less 1 0) 5 6)
Integer : 6
> ( ( let (x (read)) (y (read)) ) (add (print x) (print x) (print y) (print y)) ) read :: -17.2 Double : -17.200000 Double : -17.200000 read :: +127 Integer : 127
Integer : 127
Double : 219.600000
> ( ( let (x (rand)) ) (add (print x) (print x) ) )
Double : 0.383502
Double : 0.383502
Double : 0.767004
> quit
Process finished with exit code 0