Skip to content

gthvn1/vorth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

68 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Implement a concatenative language

Description

  • Influenced by:

  • run: v run . -c -i examples/arithmetic.vorth

    • -c is to compile the code. That means you can run the test: ./examples/arithmetic
  • or with debug logs: v run . -c -i -d 5 examples/arithmetic.vorth

  • The language is case insensitive

List of operators

Operators stack state Description
+ a b -- (a + b)
* a b -- (a * b)
- a b -- (a - b)
= a b -- Flag True if a == b, False otherwise
!= a b -- Flag True if a != b, False otherwise
< a b -- Flag True if a < b, False otherwise
> a b -- Flag True if a > b, False otherwise
. a --
divmod a b -- (a / b) (a % b)
dup a -- a a
integer -- a integer is i64
load a -- mem[a] Put on the stack the value at mem[a]
not Flag -- not Flag Inverse True and False
store v a -- Store v in mem[a]
swap a b -- b a
Operators stack state Description
if flag -- If True then it continues, otherwise
else -- it jumps to execute the then. If then
end -- is reached it jumps to end.
Operators stack state Description
while -- If True then do continues, otherwise
do flag -- it jumps right after the done. Done
done -- jumps to while.

Example

~/devel/vlang-forth master*
❯ v run . -c -i -d 5 examples/arithmetic.vorth
log level is set to 5
2024-03-08 22:05:41.692216 [DEBUG] ==== LOADING PROGRAM
2024-03-08 22:05:41.692242 [DEBUG] // arithmetic.vorth
2024-03-08 22:05:41.692245 [DEBUG]
2024-03-08 22:05:41.692249 [DEBUG] 12 30 + .      // should print 42
2024-03-08 22:05:41.692251 [DEBUG] 6 9 - .        // should print -3
2024-03-08 22:05:41.692254 [DEBUG] 12 dup + .     // should print 24
2024-03-08 22:05:41.692256 [DEBUG] 15 10 - .      // should print 5
2024-03-08 22:05:41.692258 [DEBUG] 15 10 swap - . // should print -5
2024-03-08 22:05:41.692261 [DEBUG] 5 dup mul .    // it is the square operation, 25
2024-03-08 22:05:41.692264 [DEBUG] 8 0 3 - * .    // should print -24
2024-03-08 22:05:41.692267 [DEBUG] 117 17 divmod . . // 117 17 -- 6 15
2024-03-08 22:05:41.692270 [DEBUG]                   // where 6 is the quotient and 15 the reminder
2024-03-08 22:05:41.692275 [DEBUG]                   // So should print 15 6
2024-03-08 22:05:41.692278 [DEBUG]
2024-03-08 22:05:41.692287 [DEBUG] ==== PROGRAM LOADED
42
-3
24
5
-5
25
-24
15
6

~/devel/vlang-forth master*
❯ ./examples/arithmetic
42
-3
24
5
-5
25
-24
15
6
  • others examples are not all ready. Check comments in the files.

Goals

  • start with simples instructions to do arithmetic
  • compile code
  • update lexer to recognize true, false, eq, neq
  • implement new tokens ^^
  • allow comments
  • add conditionnal if
  • add while loop
  • manage memory
  • what else to be Turing complete?

Changelog

2024-03-16

  • Implement Load and Store in memory
    • memory is 1024 bytes

2024-03-13

  • Conditionnal and loop are now compiled

2024-03-12

  • Introduce the interpreted version of the while loop
    • all tests are now running in interpreted mode
    • blocks are not yet available in compiled mode

2024-03-10

  • Adding conditionnal block if ... else ... end
  • Report filename:line:col:<error msg> in case of error

2024-03-07

  • Add Eq, Neq, Dup, Swap, Mul, Divmod, Gth, Lth, Not

2024-03-06

  • Add False and True
  • Update lexer to recognize identifiers
  • Generate code for +, -, ..
    • So we can print result of addition and substraction
  • Compile an hello world program

2024-03-05

  • Add a minimal help
  • Read the code from arithmetic.vorth and print the result
  • Replace Pop by Dot to be more like Forth and translate string to ops
  • Add operations: Add, Sub, Push and Pop
  • First implement a stack in V

About

Implementing a forth like language in V ✌️

Resources

License

Stars

Watchers

Forks