Piki

From Theory of Measurements Wiki

(Redirected from Stacklang)
Jump to: navigation, search

Piki -- a stack-oriented programming language

Source code, and some documentation.

This is just a modest attempt to create a small stack-based programming language. The focus is just to study how to implement memory management and value type handling in an interpreter-based programming environment... Simplicity and ease of extending the language are also important.

At the moment, the language is in a semi-working state, in which the very basic data types are defined and simple control constructs (if, if-then-else, for loop) work, but much work is required to make the language somehow stable. At any given time, the code is most probably looking very ugly, as I prefer rapid prototyping and lots of rewriting to designing first and implementing later... So far all new features tend to shrink over time as the implementation is improved and bugs get fixed.

Some time soon a more intelligent memory management should be thought of -- maybe by implementing some readily thought, intelligent garbage collection algorithm. Wrap malloc and free so that small and probably short-lived object would live directly in the stack and larger objects with longer lifespan would be allocated in the heap. This might possibly reduce i/o overhead in the garbage collection because the allocated objects could possibly be located a bit closer to each other compared to every small object being allocated and freed in the heap.


Some recent updates:

  • A prototype for the network socket interface was implemented. Reading from a UDP server socket works. I am currently rewriting the whole socket and file interfaces as they are a bit messy at the moment.
  • File objects. Opening a file and reading it works. Subject to rewriting, though.
  • Basic facilities to create string operations are there, at the moment, I did string concatenation just to see if it could work
  • Recursion works -- at least a recursive fibonacci numbers function works
  • Rewrote the evaluator, now it is a bit more consistent. Previously conditional expressions did not work as the evaluator evaluated the true and else clauses before testing the condition, making the evaluation of if-then and if-then-else expressions senseless

My current to-do list is

  • Primitive data types
    • hash tables
    • lists
    • arrays
  • Local namespaces

Example code that seems to work

Opening and reading stuff from file called kakka.dat:

"kakka.dat" fopen fread

Fibonacci numbers!

<< dup 1 lte
  << >>
  << dup 1 - fib 
     swap 2 - fib + 
  >> ifte
>>
fib sto

A simple loop

<< x 2 + >> body sto
1 5 'x 'body rcl for

A nested function

<< 1 + << 2 + << 3 + >> >> >>  'nested sto

ossi at kavaro dot com

Personal tools