From dd6d342dd43ad9e71f1b3848f8bee225df0b18cb Mon Sep 17 00:00:00 2001 From: sjs Date: Wed, 20 May 2009 11:39:21 -0700 Subject: [PATCH] [NEW] simple VB style for loop, f x = 1 >> 5 s = s + x e --- compiler.rb | 38 ++++++++++++++++++++++++++++++++++++++ test.code | 17 +++++++++++------ 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/compiler.rb b/compiler.rb index 6c582f8..391d04b 100644 --- a/compiler.rb +++ b/compiler.rb @@ -144,6 +144,8 @@ class Compiler until_stmt when 'r' repeat_stmt + when 'f' + for_stmt when 'b' break_stmt else @@ -223,6 +225,38 @@ class Compiler emit_label(end_label) end + # s = 0 + # f x = 1 >> 5 + # s = s + x + # e + def for_stmt + match('f') + start_label = unique_label(:for) + end_label = unique_label(:endfor) + counter = "[#{get_name}]" + match('=') + expression # initial value + x86_sub(:eax, 1) # pre-decrement because of the + # following pre-increment + x86_mov(counter, :eax) # stash the counter in memory + match('>'); match('>') + expression # final value + skip_any_whitespace + x86_push(:eax) # stash final value on stack + final = '[esp]' + emit_label(start_label) + x86_mov(:ecx, counter) # get the counter + x86_add(:ecx, 1) # increment + x86_mov(counter, :ecx) # store the counter + x86_cmp(final, :ecx) # check if we're done + x86_jz(end_label) # if so jump to the end + block(end_label) # otherwise execute the block + match('e') + x86_jmp(start_label) # lather, rinse, repeat + emit_label(end_label) + x86_add(:esp, 4) # clean up the stack + end + def break_stmt match('b') if @break_stack.empty? @@ -466,6 +500,10 @@ class Compiler emit("idiv #{op}") end + def x86_inc(op) + emit("inc #{op}") + end + def x86_push(reg) emit("push #{reg}") end diff --git a/test.code b/test.code index b6f83b3..5dfcd36 100644 --- a/test.code +++ b/test.code @@ -4,11 +4,11 @@ somethinglong=65536 x=5*(3-5) c=a-1 d=-1 -f=x*2+2 -g=f-27/9 -h=g-8/2 -j=h-4*(5+5+5) -k=h+85 +g=x*2+2 +h=g-27/9 +j=h-8/2 +k=j-4*(5+5+5) +m=k+85 i 1 d=3 @@ -39,10 +39,15 @@ u 1 e r - cc = cc * 2 + cc = c * 2 i 1 b e e +s=0 +f x = 1 >> 5 + s = s + x +e + xitcode=a-a