项目作者: GithubPrankster

项目描述 :
The (Extremely) Minimal Forth Interpreter in C.
高级语言: C
项目地址: git://github.com/GithubPrankster/forward.git
创建时间: 2020-05-29T17:48:07Z
项目社区:https://github.com/GithubPrankster/forward

开源协议:MIT License

下载


Forth Oriented Rational Wacky Arithmetic Reason Determinator

An interpreter of the bare essentials of Forth in 150~ lines.
Supported are the usual arithmetic operations, a few stack/print ones and the heart of Forth, names. Support for arrays and branching has yet to be figured out.

Compiling & Time

gcc -Ofast -march=native -mtune=native -s -static -static-libgcc forward.c -o forward

Compilation on a AMD FX-8350 yielded a time of:

  1. real 0m0.163s
  2. user 0m0.118s
  3. sys 0m0.045s

Example

  1. : STAR 42 EMIT ;
  2. : LINE STAR STAR STAR STAR STAR ;
  3. LINE CR STAR CR LINE CR STAR CR STAR CR

This shall give you a nice F.

Implementation of Names

I made a structure which contains the name itself and what follows after until ;. Then whenever the evaluator finds a letter, it checks against the currently stored names, and if a match is found, it evaluates the name’s buffer. The fact this process is recursive highlights one of the powers of Forth. Currently however, replacement of pre-defined names is not implemented. Be careful making names that are already pre-defined in the interpreter. Or not, that’s your call.