Giter Club home page Giter Club logo

nf's Introduction

nf

A minimal programming language, originally designed as an interactive environment for my os/64. Inspired by Forth, but not compatible. Easily portable to different platforms, including DOS and bare-bones x86. Main characteristics: imperative, stack-based, interpreted, compiled to bytecode.

Building for Linux / MacOS / POSIX

$ make
$ make test
$ ./build/nf
>>> "Hello World!!!\n" printf
Hello World!!!
>>> <Ctrl+D>
$

Building for DOS / BIOS

  • Install DOSBox and QEMU

  • Install Turbo C 2.01 to C:\TC

  • Install NASM to C:\NASM

  • Compile:

    Z:\>mount d ~/nf
    Z:\>D:
    D:\>MAKE -fMAKEFILE.DOS
    D:\>BUILD\NF.COM
    >>> exit
    D:\>
    
  • Create and test the bootable version in QEMU:

    $ make qemu
    
  • Optionally, if you have a PC with support for booting from USB in BIOS mode, and an unused USB stick:

    $ dd if=build/nf_x86.img of=/dev/<usb-stick>
    

Usage

Printing

>>> cr

>>>

>>> 1 2 3 . . . cr
3 2 1
>>>

>>> 1 2 3 4 5
>>> .s
1 2 3 4 5
>>>

>>> "rld!!!" "llo wo" "He" "%s%s%s\n" printf . cr
Hello world!!!
15
>>>

Stack manipulation

>>> 1 2 3 .s
1 2 3
>>> dup .s
1 2 3 3
>>> drop .s
1 2 3
>>> swap .s
1 3 2
>>> rot .s
3 2 1
>>> over .s
3 2 1 2
>>>

Operators

>>> 4 2 >= . cr
1
>>> 4 2 < . cr
0
>>> 1 0 && . cr
0
>>> 1 2 || . cr
1
>>> 0 ! . cr
1
>>> 1 2 & . cr
0
>>> 1 2 | . cr
3
>>>

Conditionals

>>> 1 2 < if
...   "ok\n" printf
... then
ok

>>> 4 6 > if
...     "err\n" printf
... else
...     "ok\n" printf
... then
ok
>>>

Loops

>>> 5 do
...     dup
... while
...     dup .
...     1 -
... repeat cr
5 4 3 2 1
>>>

>>> 0 do
...     dup .
...     1 + dup
... 5 == until cr
0 1 2 3 4
>>>

Variables and subroutines

>>> 123 "x" var
>>> x "x = %d\n" printf
x = 123
>>> 321 "x" :=
>>> x "x = %d\n" printf
x = 321
>>>

>>> :
...   2 *
... ; "double" def
>>> 5 double . cr
10
>>>

Example application (os64)

\ ------------------------------------
\ /apps/cat.nf - display file contents
\ ------------------------------------

\ global variables

32 "size" var
0 "buf" var
-1 "fd_in" var
-1 "fd_out" var
0 "count" var
"/dev/vt" "path_out" var

\ cleanup and exit

:
        buf 0 != if buf sys-free then
        fd_in 0 >= if fd_in sys-close drop then
        fd_out 0 >= if fd_out sys-close drop then
        sys-exit
; "cat-exit" def

\ check command line arguments

argc 2 < if
        "usage: cat.nf <filename>\n" printf drop
        -1 cat-exit
then

\ allocate buffer

size sys-malloc "buf" :=
buf 0 == if -1 cat-exit then

\ open input file

1 argv sys-open "fd_in" :=
fd_in 0 < if -1 cat-exit then

\ open virtual terminal device

path_out sys-open "fd_out" :=
fd_out 0 < if -1 cat-exit then

\ copy from input file to terminal until end of file

do
        size buf fd_in sys-read "count" :=
        count 0 < if -1 cat-exit then
        count 0 == if 0 cat-exit then
        count buf fd_out sys-write drop
0 until

nf's People

Contributors

luke8086 avatar

Watchers

James Cloos 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.