v icon indicating copy to clipboard operation
v copied to clipboard

Interpreter doesn't display bit shifted values in some cases

Open nm17 opened this issue 2 years ago • 5 comments

V version: 0.2.4 509367b OS: Linux

What did you do?

 ____    ____ 
 \   \  /   /  |  Welcome to the V REPL (for help with V itself, type  exit , then run  v help ).
  \   \/   /   |  Note: the REPL is highly experimental. For best V experience, use a text editor, 
   \      /    |  save your code in a  main.v  file and execute:  v run main.v 
    \    /     |  V 0.2.4 509367b . Use  list  to see the accumulated program so far.
     \__/      |  Use Ctrl-C or  exit  to exit, or  help  to see other available commands.

>>> 4 >> 1
2
>>> 4 << 1
>>> println(4 << 1)
8
>>> typeof(4 >> 1).name
int literal
>>> typeof(4 << 1).name
>>> println(typeof(4 << 1).name)
int literal

What did you expect to see?

Expression 4 << 1 should print 8 in the terminal and typeof(4 << 1) should be displayed the same as typeof(4 >> 1)

What did you see instead?

Weird behavior that doesn't make sense. (AFAIK, << should be a simple bit shift operation)

nm17 avatar Apr 07 '22 19:04 nm17

Idk if this is related, but << is also used to append to arrays. Maybe interpreter is getting confused?

depperm avatar Apr 28 '22 03:04 depperm

Idk if this is related, but << is used to spend to arrays. Maybe interpreter is getting confused?

Yes, the problem lies here: https://github.com/vlang/v/blob/master/cmd/tools/vrepl.v#L405 The REPL assumes that all lines containing << are array appendices, so it considers them a statement instead of an expression. I'm not sure how to fix this though without some ugly workarounds.

Wertzui123 avatar May 29 '22 11:05 Wertzui123

The best way to "fix" this would be to finish the "new" interpreter, and replace the REPL with that.

JalonSolov avatar May 29 '22 12:05 JalonSolov

Isn't there a way to first parse the code, then analyze the AST and finally compile it to C code, which is then executed?

Wertzui123 avatar May 29 '22 14:05 Wertzui123

@JalonSolov

Is there any project currently in development for the new interpreter? I ask because I'm interested in contributing! =)

viniciusfdasilva avatar Jan 15 '24 17:01 viniciusfdasilva