init ps6
This commit is contained in:
45
exercises/06/vslc/vsl_programs/Makefile
Normal file
45
exercises/06/vslc/vsl_programs/Makefile
Normal file
@@ -0,0 +1,45 @@
|
||||
VSLC := ../src/vslc
|
||||
AS := gcc
|
||||
|
||||
# This updated makefile calls the relevant print for each assignment
|
||||
# The added target compile will attempt to compile all vsl source files,
|
||||
# for ps5 and ps6. Other programs are mostly not interesting after the their
|
||||
# targeted assignment.
|
||||
|
||||
# Call `vslc -h` to see the available flags, and call `vslc [flags] < file.vsl`
|
||||
# to compile a single file.
|
||||
|
||||
PS2_EXAMPLES := $(patsubst ps2-parser/%.vsl, ps2-parser/%.ast, $(wildcard ps2-parser/*.vsl))
|
||||
PS3_EXAMPLES := $(patsubst ps3-simplify/%.vsl, ps3-simplify/%.sast, $(wildcard ps3-simplify/*.vsl))
|
||||
PS4_EXAMPLES := $(patsubst ps4-symtab/%.vsl, ps4-symtab/%.sym, $(wildcard ps4-symtab/*.vsl))
|
||||
PS5_EXAMPLES := $(patsubst ps5-codegen1/%.vsl, ps5-codegen1/%.S, $(wildcard ps5-codegen1/*.vsl))
|
||||
PS6_EXAMPLES := $(patsubst ps6-codegen2/%.vsl, ps6-codegen2/%.S, $(wildcard ps6-codegen2/*.vsl))
|
||||
|
||||
PS5_OBJECTS := $(PS5_EXAMPLES:.S=.bin)
|
||||
PS6_OBJECTS := $(PS6_EXAMPLES:.S=.bin)
|
||||
# OBJECTS := $(PS5_OBJECTS) $(PS4_EXAMPLES:.sym=.bin) $(PS3_EXAMPLES:.sast=.bin) $(PS2_EXAMPLES:.ast=.bin)
|
||||
OBJECTS := $(PS5_OBJECTS) $(PS6_OBJECTS)
|
||||
all: $(PS2_EXAMPLES) $(PS3_EXAMPLES) $(PS4_EXAMPLES) $(PS5_EXAMPLES) $(PS6_EXAMPLES)
|
||||
ps2: $(PS2_EXAMPLES)
|
||||
ps3: $(PS3_EXAMPLES)
|
||||
ps4: $(PS4_EXAMPLES)
|
||||
ps5: $(PS5_EXAMPLES)
|
||||
|
||||
%.ast: %.vsl
|
||||
$(VSLC) -t -q < $^ > $@ 2> $@
|
||||
%.sast: %.vsl
|
||||
$(VSLC) -T -q < $^ > $@ 2> $@
|
||||
%.sym: %.vsl
|
||||
$(VSLC) -s -q < $^ > $@ 2> $@
|
||||
%.S: %.vsl
|
||||
$(VSLC) < $^ > $@
|
||||
# This target is only tested on x86-linux
|
||||
%.bin: %.S
|
||||
$(AS) -no-pie -o $@ $^
|
||||
|
||||
ps5-compile: $(PS5_OBJECTS)
|
||||
ps6-compile: $(PS6_OBJECTS)
|
||||
compile: $(OBJECTS)
|
||||
|
||||
clean:
|
||||
-rm -r */*.ast */*.sast */*.sym */*.bin */*.S
|
||||
14
exercises/06/vslc/vsl_programs/ps2-parser/assignments.vsl
Normal file
14
exercises/06/vslc/vsl_programs/ps2-parser/assignments.vsl
Normal file
@@ -0,0 +1,14 @@
|
||||
// checking that comments are ignored
|
||||
|
||||
// This program checks the assignment operators
|
||||
|
||||
func main()
|
||||
begin
|
||||
var a
|
||||
a := 3
|
||||
a += 1
|
||||
a /= 2
|
||||
a *= 32
|
||||
a -= 2
|
||||
print a
|
||||
end
|
||||
@@ -0,0 +1,8 @@
|
||||
func add(a, b) begin
|
||||
return a + b
|
||||
end
|
||||
|
||||
func main()
|
||||
begin
|
||||
print add(40, 2)
|
||||
end
|
||||
3
exercises/06/vslc/vsl_programs/ps2-parser/helloworld.vsl
Normal file
3
exercises/06/vslc/vsl_programs/ps2-parser/helloworld.vsl
Normal file
@@ -0,0 +1,3 @@
|
||||
func main() begin
|
||||
print "Hello, World!"
|
||||
end
|
||||
25
exercises/06/vslc/vsl_programs/ps2-parser/if_else.vsl
Normal file
25
exercises/06/vslc/vsl_programs/ps2-parser/if_else.vsl
Normal file
@@ -0,0 +1,25 @@
|
||||
func main()
|
||||
begin
|
||||
var a, b, c, d
|
||||
c := 1
|
||||
a := 3
|
||||
b := a + c // 4
|
||||
d := a * 100 + 50
|
||||
print "a", a
|
||||
print "b", b
|
||||
print "c", c
|
||||
print "d", d
|
||||
if a = 14 then
|
||||
print 1, "N", d / 5 + a, "RPR", a, "TERS "
|
||||
else
|
||||
print "COMP", c, "L", "ERS "
|
||||
|
||||
print b, "R", a, " "
|
||||
|
||||
if a < b then
|
||||
if d > 42 then
|
||||
print b, "W", d, "ME"
|
||||
else
|
||||
print "L", b, "M", c
|
||||
// A dangling else, what could go wrong?
|
||||
end
|
||||
19
exercises/06/vslc/vsl_programs/ps2-parser/variables.vsl
Normal file
19
exercises/06/vslc/vsl_programs/ps2-parser/variables.vsl
Normal file
@@ -0,0 +1,19 @@
|
||||
|
||||
var global_var
|
||||
|
||||
func my_func(param)
|
||||
begin
|
||||
var local_var, local_var2
|
||||
local_var := 1
|
||||
end
|
||||
|
||||
var glob1, glob2
|
||||
|
||||
func main()
|
||||
begin
|
||||
var main_local_var
|
||||
begin
|
||||
var main_local_nested_var
|
||||
main_local_nested_var := main_local_var
|
||||
end
|
||||
end
|
||||
10
exercises/06/vslc/vsl_programs/ps2-parser/while.vsl
Normal file
10
exercises/06/vslc/vsl_programs/ps2-parser/while.vsl
Normal file
@@ -0,0 +1,10 @@
|
||||
// check parsing of do-while loop
|
||||
|
||||
func main()
|
||||
begin
|
||||
var i
|
||||
i := 2
|
||||
while i < 9000 do
|
||||
i := i * i
|
||||
print i
|
||||
end
|
||||
@@ -0,0 +1,9 @@
|
||||
|
||||
func main() begin
|
||||
var a, b
|
||||
a := 1 + 2 + 4 + 5 + 6 + 7 + 8 + 9
|
||||
b := (10 + 10 * 4) * (2 + 2 * (1 + 1)) / 10 + 2 * 5 + 6 / 3
|
||||
|
||||
if a = b then
|
||||
print "The answer is", b
|
||||
end
|
||||
16
exercises/06/vslc/vsl_programs/ps3-simplify/lists.vsl
Normal file
16
exercises/06/vslc/vsl_programs/ps3-simplify/lists.vsl
Normal file
@@ -0,0 +1,16 @@
|
||||
|
||||
func my_func(a, b, c, d, e, f, g, h) begin
|
||||
var i, j, k, l, m
|
||||
|
||||
i := a + b + d
|
||||
|
||||
if i = f then begin
|
||||
print "hmmm"
|
||||
end
|
||||
end
|
||||
|
||||
func main() begin
|
||||
var n, o, p, q, r, s, t, u, v, w
|
||||
n := 5
|
||||
n += my_func(1, 2, 3, 5, 8, 13, 21, 34)
|
||||
end
|
||||
11
exercises/06/vslc/vsl_programs/ps4-symtab/globals.vsl
Normal file
11
exercises/06/vslc/vsl_programs/ps4-symtab/globals.vsl
Normal file
@@ -0,0 +1,11 @@
|
||||
var global_var0, global_var1
|
||||
|
||||
func my_func(param0, param1) begin
|
||||
var a
|
||||
return 0
|
||||
end
|
||||
|
||||
func main() begin
|
||||
var a
|
||||
print "a string"
|
||||
end
|
||||
24
exercises/06/vslc/vsl_programs/ps4-symtab/shadow.vsl
Normal file
24
exercises/06/vslc/vsl_programs/ps4-symtab/shadow.vsl
Normal file
@@ -0,0 +1,24 @@
|
||||
|
||||
func main() begin
|
||||
var a, b
|
||||
a := 1
|
||||
begin
|
||||
var a
|
||||
a := 2
|
||||
b := 40
|
||||
begin
|
||||
var a
|
||||
a := b + 2
|
||||
print a, b
|
||||
end
|
||||
print a
|
||||
begin
|
||||
var b
|
||||
b := 38
|
||||
a := b + 3
|
||||
print a, b
|
||||
end
|
||||
print a
|
||||
end
|
||||
print b
|
||||
end
|
||||
@@ -0,0 +1,9 @@
|
||||
func add(a, b) begin
|
||||
print "adding", a, "and", b
|
||||
return a + b
|
||||
end
|
||||
|
||||
func main()
|
||||
begin
|
||||
print 2 + add(40, 2) + 2
|
||||
end
|
||||
@@ -0,0 +1,3 @@
|
||||
func main() begin
|
||||
print "Hello, World!"
|
||||
end
|
||||
30
exercises/06/vslc/vsl_programs/ps5-codegen1/ps5.vsl
Normal file
30
exercises/06/vslc/vsl_programs/ps5-codegen1/ps5.vsl
Normal file
@@ -0,0 +1,30 @@
|
||||
// This program tests activation records, function call and return
|
||||
func funcall ()
|
||||
begin
|
||||
var x,y,z
|
||||
x := 5
|
||||
y := 10
|
||||
print "Calling my_function with parameters", x, y
|
||||
z := my_function ( x, y )
|
||||
print "The returned result is", z
|
||||
z := my_other_function ()
|
||||
print "The other returned result is", z
|
||||
return 0
|
||||
end
|
||||
|
||||
func my_function ( s, t )
|
||||
begin
|
||||
var u
|
||||
u := s*s + t*t
|
||||
print "Parameter s is", s
|
||||
print "Parameter t is", t
|
||||
print "The sum of their squares is", u
|
||||
return u
|
||||
end
|
||||
|
||||
func my_other_function ()
|
||||
begin
|
||||
var x
|
||||
x := 42
|
||||
return x
|
||||
end
|
||||
24
exercises/06/vslc/vsl_programs/ps5-codegen1/shadow.vsl
Normal file
24
exercises/06/vslc/vsl_programs/ps5-codegen1/shadow.vsl
Normal file
@@ -0,0 +1,24 @@
|
||||
|
||||
func main() begin
|
||||
var a, b
|
||||
a := 1
|
||||
begin
|
||||
var a
|
||||
a := 2
|
||||
b := 40
|
||||
begin
|
||||
var a
|
||||
a := b + 2
|
||||
print a, b
|
||||
end
|
||||
print a
|
||||
begin
|
||||
var b
|
||||
b := 38
|
||||
a := b + 3
|
||||
print a, b
|
||||
end
|
||||
print a
|
||||
end
|
||||
print b
|
||||
end
|
||||
20
exercises/06/vslc/vsl_programs/ps6-codegen2/euclid.vsl
Normal file
20
exercises/06/vslc/vsl_programs/ps6-codegen2/euclid.vsl
Normal file
@@ -0,0 +1,20 @@
|
||||
func euclid ( a, b )
|
||||
begin
|
||||
if a < 0 then a := -a
|
||||
if b < 0 then b := -b
|
||||
if gcd ( a, b ) > 1 then
|
||||
print "Greatest common divisor of", a, "and", b, "is", gcd ( a, b )
|
||||
else
|
||||
print a, "and", b, "are relative primes"
|
||||
return 0
|
||||
end
|
||||
|
||||
func gcd( a, b )
|
||||
begin
|
||||
var g
|
||||
if b > 0 then
|
||||
g := gcd ( b, a - ((a/b)*b) )
|
||||
else
|
||||
g := a
|
||||
return g
|
||||
end
|
||||
21
exercises/06/vslc/vsl_programs/ps6-codegen2/newton.vsl
Normal file
21
exercises/06/vslc/vsl_programs/ps6-codegen2/newton.vsl
Normal file
@@ -0,0 +1,21 @@
|
||||
// Approximate square root by the Newton/Raphson method for f(x) = x^2 - n
|
||||
// f(x) = x^2 - n = 0
|
||||
// f'(x) = 2x
|
||||
// x{n+1} = x{n} - (x^2-n) / 2x
|
||||
|
||||
func newton ( n )
|
||||
begin
|
||||
print "The square root of", n, "is", improve ( n, 1 )
|
||||
return 0
|
||||
end
|
||||
|
||||
func improve ( n, estimate )
|
||||
begin
|
||||
var next
|
||||
next := estimate - ( (estimate * estimate - n) / ( 2 * estimate ) )
|
||||
if next - estimate = 0 then
|
||||
// Integer precision converges at smallest int greater than the square
|
||||
return next-1
|
||||
else
|
||||
return improve ( n, next )
|
||||
end
|
||||
23
exercises/06/vslc/vsl_programs/ps6-codegen2/while_test.vsl
Normal file
23
exercises/06/vslc/vsl_programs/ps6-codegen2/while_test.vsl
Normal file
@@ -0,0 +1,23 @@
|
||||
// This program is a simple test of while loops, counting down from 19 to 0
|
||||
// and skipping 10 (if continue is implemented)
|
||||
|
||||
func while_test ()
|
||||
begin
|
||||
var a
|
||||
a := 20
|
||||
print a
|
||||
if a > 0 then print "foobar"
|
||||
while a > 0 do
|
||||
begin
|
||||
if a = 10 then
|
||||
begin
|
||||
a -= 1
|
||||
print "Skip..."
|
||||
continue
|
||||
end
|
||||
else
|
||||
a -= 1
|
||||
print a
|
||||
end
|
||||
return 0
|
||||
end
|
||||
Reference in New Issue
Block a user