Writing an interpreter and compiler in Julia

View previous topic View next topic Go down

Writing an interpreter and compiler in Julia

Post  DIN99 on Fri May 01, 2015 7:53 pm

Hi! I see that this forum is not very active anymore, but I guess that’s the fate of a specialized forum like this one... Anyway, I’m working on a Piet interpreter and compiler written in Julia.

It already runs quite well, digesting assembly-like code, and some helper constructs to enable loops and branches. Converting the ‘opcodes’ to colors works fine as well.
It’s more like a programming exercise in Julia for me, as I’m learning the language, and I hope I get a working package done in the end. I’m also working on a way to change the color palette on demand. For example for Piet programs that are red-green color deficiency friendly Wink And for people with normal vision who hate the fact that the original colors are distributed perceptually uneven. E.g. I’m trying to implement an optional palette that has all colors on a certain brightness level being actually the same brightness. Sorry, I’m also a color science geek, so I had to address this issue. Wink

So.... to show how the debugging output looks at the moment, here is an “opcode” snippet with the according debugging output

It shows the stacks for every operation, the original state, the ‘split-up’ state when values got popped off the stack for the operator to work, and the resulting stack. The little dot next to each stack just marks the top of the stack.

This is the part of the Piet program that’s reflected in the debugging output farther below:
Code:

PTR
J 0*2 1*3
*2
DUP
DUP
MUL
3
PSH
1
PSH
2
PSH
SUB
ROL

And the debugging output:

Code:

opcode  :  === PTR ===
pre-stack  : [2865,2,0]·
pre|op stack: [2865,2]· [0]·
DP was  :  (0) R
DP is now:  (0) R
post-stack  : [2865,2]·
Instr.:12                        === TICK #350 ===
jump    :  === J 0*2 1*3 ===
DP=0 => continue after *2 @ 14
Instr.:14                        === TICK #350 ===
opcode  :  === DUP ===
pre-stack  : [2865,2]·
pre|op stack: [2865]· [2]·
post-stack  : [2865,2,2]·
Instr.:15                        === TICK #351 ===
opcode  :  === DUP ===
pre-stack  : [2865,2,2]·
pre|op stack: [2865,2]· [2]·
post-stack  : [2865,2,2,2]·
Instr.:16                        === TICK #352 ===
opcode  :  === MUL ===
pre-stack  : [2865,2,2,2]·
pre|op stack: [2865,2]· [2,2]·
post-stack  : [2865,2,4]·
Instr.:17                        === TICK #353 ===
Number  : === 3 ===
Instr.:18                        === TICK #353 ===
opcode  :  === PSH ===
pre-stack  : [2865,2,4]·
pre|op stack: [2865,2,4]· [3]·
post-stack  : [2865,2,4,3]·
Instr.:19                        === TICK #354 ===
Number  : === 1 ===
Instr.:20                        === TICK #354 ===
opcode  :  === PSH ===
pre-stack  : [2865,2,4,3]·
pre|op stack: [2865,2,4,3]· [1]·
post-stack  : [2865,2,4,3,1]·
Instr.:21                        === TICK #355 ===
Number  : === 2 ===
Instr.:22                        === TICK #355 ===
opcode  :  === PSH ===
pre-stack  : [2865,2,4,3,1]·
pre|op stack: [2865,2,4,3,1]· [2]·
post-stack  : [2865,2,4,3,1,2]·
Instr.:23                        === TICK #356 ===
opcode  :  === SUB ===
pre-stack  : [2865,2,4,3,1,2]·
pre|op stack: [2865,2,4,3]· [2,1]·
post-stack  : [2865,2,4,3,-1]·
Instr.:24                        === TICK #357 ===
opcode  :  === ROL ===
pre-stack  : [2865,2,4,3,-1]·
pre|op stack: [2865,2,4]· [-1,3]·
post-stack  : [2,4,2865]·
Instr.:25                        === TICK #358 ===

Don’t be irritated by the strange lines
Code:

jump    :  === J 0*2 1*3 ===
DP=1 => continue after *2 @ 14

this just outputs a jump instruction and the jump conditions.
J 0*2 1*3 just means Jump if DP=0 to *2 (label 2), if DP=1 to *3

It’s just a realization of the effect of the PTR instruction, for easy debugging. Translating this into 2-d codel layout shouldn’t be too problematic.

I added the verbose stack output to my program because I’ve seen a lot of questions about how e.g. the roll operation actually works in Piet. A clear visual output should be helpful for both learning and debugging Piet programs.

I hope it’s somewhat interesting to somebody here Wink

Cheers!
avatar
DIN99

Posts : 4
Join date : 2015-04-17
Location : Germany

View user profile

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum