Giter Site home page Giter Site logo

vm's Introduction

Stack Machine using Nim lang

19,0,10,2,8,17,10,10,1,20,19,0,19,0,10,1,3,18,0,1,4,20,10,2,18,0,1

proc fact(n: int): int =
  return if n < 2: 1
             else: n * fact(n - 1)
# if n < 2: 1 else:
Arg       # 0
0         # 1
Push      # 2
2         # 3
Lt        # 4
JumpFalse # 5
10        # 6
Push      # 7
1         # 8
Ret       # 9

# n * fact(n - 1)
Arg       # 10
0         # 11
Arg       # 12
0         # 13
Push      # 14
1         # 15
Sub       # 16
Call      # 17
0         # 18
1         # 19
Mul       # 20
Ret       # 21

# proc fact(n: int): int
Push      # 22 - entry
2         # 23 - input value
Call      # 24
0         # 25
1         # 26
[tr3nx@box hvm]$ nim c -r vm.nim 
Hint: used config file '/etc/nim/nim.cfg' [Conf]
Hint: system [Processing]
Hint: widestrs [Processing]
Hint: io [Processing]
Hint: vm [Processing]
CC: vm.nim
Hint:  [Link]
Hint: operation successful (14708 lines compiled; 0.255 sec total; 20.742MiB peakmem; Debug Build) [SuccessX]
Hint: /home/tr3nx/coding/nim/hvm/vm  [Exec]
---------------------
 ip = 24 | fp = 0
    Code | Push (9)
   Stack | @[2]
  Memory | []

---------------------
 ip = 0 | fp = 3
    Code | Call (17)
   Stack | @[2, 1, 0, 27]
  Memory | []

---------------------
 ip = 2 | fp = 3
    Code | Arg (18)
   Stack | @[2, 1, 0, 27, 2]
  Memory | []

---------------------
 ip = 4 | fp = 3
    Code | Push (9)
   Stack | @[2, 1, 0, 27, 2, 2]
  Memory | []

---------------------
 ip = 5 | fp = 3
    Code | Lt (7)
   Stack | @[2, 1, 0, 27, 0]
  Memory | []

---------------------
 ip = 10 | fp = 3
    Code | JumpFalse (16)
   Stack | @[2, 1, 0, 27]
  Memory | []

---------------------
 ip = 12 | fp = 3
    Code | Arg (18)
   Stack | @[2, 1, 0, 27, 2]
  Memory | []

---------------------
 ip = 14 | fp = 3
    Code | Arg (18)
   Stack | @[2, 1, 0, 27, 2, 2]
  Memory | []

---------------------
 ip = 16 | fp = 3
    Code | Push (9)
   Stack | @[2, 1, 0, 27, 2, 2, 1]
  Memory | []

---------------------
 ip = 17 | fp = 3
    Code | Sub (2)
   Stack | @[2, 1, 0, 27, 2, 1]
  Memory | []

---------------------
 ip = 0 | fp = 8
    Code | Call (17)
   Stack | @[2, 1, 0, 27, 2, 1, 1, 3, 20]
  Memory | []

---------------------
 ip = 2 | fp = 8
    Code | Arg (18)
   Stack | @[2, 1, 0, 27, 2, 1, 1, 3, 20, 1]
  Memory | []

---------------------
 ip = 4 | fp = 8
    Code | Push (9)
   Stack | @[2, 1, 0, 27, 2, 1, 1, 3, 20, 1, 2]
  Memory | []

---------------------
 ip = 5 | fp = 8
    Code | Lt (7)
   Stack | @[2, 1, 0, 27, 2, 1, 1, 3, 20, 1]
  Memory | []

---------------------
 ip = 7 | fp = 8
    Code | JumpFalse (16)
   Stack | @[2, 1, 0, 27, 2, 1, 1, 3, 20]
  Memory | []

---------------------
 ip = 9 | fp = 8
    Code | Push (9)
   Stack | @[2, 1, 0, 27, 2, 1, 1, 3, 20, 1]
  Memory | []

---------------------
 ip = 20 | fp = 3
    Code | Ret (19)
   Stack | @[2, 1, 0, 27, 2, 1]
  Memory | []

---------------------
 ip = 21 | fp = 3
    Code | Mul (3)
   Stack | @[2, 1, 0, 27, 2]
  Memory | []

---------------------
 ip = 27 | fp = 0
    Code | Ret (19)
   Stack | @[2]
  Memory | []

2

vm's People

Contributors

tr3nx avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.