Skip to content

jacoobes/slowscript

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

60 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

slowscript

a tree-walk interpreter implemented in Kotlin that was created to learn how programming languages worked
Although not very practical to use in a real project, it is Turing complete and provides basic scripting needs.
Yes, its slow.
Made possible with http://craftinginterpreters.com/

References

log ("Hello world")
loop(var i = 0 as i < 10: i++) {
    log("Hello world")   
}
class A {

}

class littleA from A {

}
task fib(n) {
    if (num <= 1) return 1;
    return fib(n - 1) + fib( n - 2); 
}
//Native function asking question and the type of response
var ans = responseTo("What is 10 - 10?", "number")

if (ans != 0) {
 log("Idiot")
 return;
}

Features

  • HashMaps for environments
  • Implementation of Visitor Pattern
  • separate parser and lexer
    • No modes
  • primitives
    • boolean,
    • number (64 bit)
    • strings
  • Semantic Analysis
  • Recursive Descent Parser
  • Classes
    • Inheritance
    • super keyword
    • this keyword
  • Functions
  • Lexical Scoping
  • Global variables
  • Closures
  • Lexically scoped variables
  • if / else (No else if)
  • Comments and comment blocks
  • Short-hands for expressions ( +=, ++, --)
  • return statements
  • ternary operators
  • loops (loop, while)
  • Kotlin and JS syntax inspiration
  • Object getters and setters
  • multi-line strings
  • truthy values
  • short circuiting evaluation
  • Native functions :
    • asking for user response in terminal
    • stopwatch
    • exitProcess
  • No implicit primitive type conversions (number to number, string to string only)

Not Featured

  • Module system
  • multi-file support
  • standard library
  • type system
  • immutability
  • speed
  • syntax highlighting (yet)
  • bitwise operators
  • typeof or instanceof operators
  • dead code elimination
  • static methods in classes
  • escape characters
  • template literals
  • no data structures

As stated above, this language was created to learn language design and is not meant for practical use

Syntax

keywords

  • super

  • class

  • return

  • var

  • task

  • false

  • true

  • null

  • if

  • else

  • loop

  • while

  • from

  • super

  • this

  • NaN

  • log

  • as

  • this

  • init

  • object

tokens

{ } ( ) ; . , : ? # ! = > <= >= < - . + ++ -- " % / /=
And many more! (tokens are similar to C / JS syntax)

Quick Docs

declare a variable -> var hi = "Hello"

  • declared but not assigned variables are null by default
  • variables that have not been declared nor defined but accessed throw undefined variable error
  • semicolons are always omitted

declare function -> task name() {}

  • returns must end in semicolon
  • nested functions are okay (closures)

declare class -> class T {}

  • classes have optional init blocks like Kotlin. use init{ #stmts | expressions #}
  • class joeWithInit {
       init {
         log("a new joe class was instatiated")
         //does not have access to 'this' properties
         //cannot return anything
         
       }
    //constructor
       object(a,b) {
         this.a = a
         this.b = b
         }
       eatChicken() {
         log(this.a + " ate" + " chicken") 
       }
    
    }
    

extending class -> class E from T{}
constructor -> object( #parameters#) {}
while -> while(expression) {}
loop -> loop(as:) #infinite loop#, simple loop ->loop (var i = 0 as i < 10: i++)
comments -> // line specific
comment blocks -> # comments #
print to terminal -> log()

  • Automatically appends new line (\n) (equivalent to println())

declare method in class -> name(#parameters#){}
ternary operators -> condition expression ? expression evaluated if true : expression evaluated if false

  • Cannot compound ternary operators

Short Circuiting -> expression || expression

  • Evaluates first truthy values
  • NaN and null are falsy

A few things I want to complete

  • VScode extension in the process
  • logo
  • a cmdlet or something to make this easier to run then direct jar
  • usable download on this repo

About

a multipurpose, interpreted language

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published