tsoding / porth

It's like Forth but in Python
629 stars 50 forks source link

`porth sim` and `porth com -r` diverges in code execution #62

Closed drocha87 closed 3 years ago

drocha87 commented 3 years ago

The code below seems to generate the expected output in simulation mode but don't output anything in compilation mode.

include "std.porth"

"abcdefghijklmnopqrstuvxyz\0"

mem     swap .64
mem + 1 swap .

mem     ,64 print // string address in memory 
mem + 1 ,   print // 26 string length
mem ,64 ,   print // 97 (a) 

Simulation output: ./porth.py sim

31
26
97

Compilation output is empty. ./porth.py com -r

cg-jl commented 3 years ago

I don't know, but that same code doesn't work in either. The simulated one errors at pop from empty list and the compiled one segfaults: image EDIT: as my prompt shows that there have been changes to my local version of the repo, here is the git status output so that you can see that I only changed some of the highlights for myself: image

zrthxn commented 3 years ago

It's possible that this might be because of mixing the load store for single byte and 8-byte words. I'm taking a look. Hopefully it's not because of something I did wrong :sweat_smile:

zrthxn commented 3 years ago

So, the correct program to do what this code is trying to do would be the following (I think)

include "std.porth"

"abcdefghijklmnopqrstuvxyz\0"

mem     swap .64
mem 8 + swap .

mem     ,64          print // string address in memory
mem 8 + ,            print // 26 string length
mem ,64 cast(ptr) ,  print // 97 (a)

The order of operators (+ <int> instead of <int> +) was wrong and since the we are storing the pointer as a 64-bit value, we have to increment by 8 bytes (not 1) while reading from memory.

drocha87 commented 3 years ago

@zrthxn the main issue is that the code should fail or succeed in both simulation and compilation. If the code is wrong as you pointed out it should fail in simulation as well. Maybe the implementation of the operators in simulation and compilation is diverging.

zrthxn commented 3 years ago

@drocha87 Well at the moment with the type checking your program by itself does fail on both, with test.porth:6:14: ERROR: invalid argument type for STORE intrinsic. However if type-checking was to be disabled then the simulation output is IndexError: pop from empty list after printing 13, however the compiled version just segfaults... so what you say can actually be happening

drocha87 commented 3 years ago

I reported this issue 14 days ago. The language has changed a lot at this point. I'll close this issue because maybe it's not a valid issue anymore.