- Notifications
You must be signed in to change notification settings - Fork0
SaurScript/saurscript-lang
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Dead-simple functional
JavaScript
transpiler.
SaurScript
looks like this:
String myName="Computer"funsayHi(String name) > String {return concat("Hello", name)}String greeting= sayHi(myName)logger.log(greeting)
It compiles toJavaScript
:
constmyName="Computer";constgreeting=(function(name=myName){return"Hello "+name;})();console.log(greeting)
- Functional
- Pure
- Statically typed
- Lazy
- Easy to learn
A lot of functional programming languages use unfamiliar syntax:
myName="Computer"sayHi::String->StringsayHi name="Hello"++ namegreeting= sayHi myNamemain=print greeting
This can make it hard to learn.SaurScript
uses more familiar syntax, making it easier to learn.
To learn how to useSaurScript
we're going to create a thesaurus.
Usually you'll want to import either thecommon
orbrowser
library to start.These give you access tologger
,document
,storage
,map
,strConcat
,filter
, and other useful tools.You can also import other.saur
files.
// The common lib:@import'common'// The browser lib (this includes common, but is only for frontend dev. Don't use both):@import'browser'// Another `.saur` file:@import'myOtherFile'
Variables inSaurScript
are immutable and statically typed.
Int myVar=5
This will compile to:
constmyVar=5;
You must specify a type to create the variable. Once you create the variable, you cannot change it.
Don't think of variables as boxes where you can take things in and out.Think of the variable name as a word in our thesaurus, and the value is the synonyms.
Let's create an entry for our thesaurus:
Dictionary<String:Array<String>> entries= ["Dinosaur":"Dino","Raptor","Big Lizard"]
Here we declare a new variable namedentries
. It's type is aDictionary
.The key of the dictionary is aString
(a word), and the value is anArray
ofString
s (synonyms).
Functions are lazy. If we don't use the function, it won't show up in ourJavaScript
code.
funmyFunction(Int arg1,Float arg2) -> Bool { logger.log(arg1+ arg2)returntrue}
Since we don't use this function, ourJavaScript
file is empty.
Let's add a function to our thesaurus to get the synonyms for any given word.
funsynonyms(String word) -> Array<String> {return entries[word]}
Classes are an experimental feature ofSaurScript
. They mix theFunctional
andObject Oriented
paradigms.
@classMyClass @propertyInt id=0funwhoami() -> String {return"A Computer" }@endclass@newMyClass myInstance(5)logger.log(myInstance.whoami())
This will compile to:
functionMyClass(id=0){this.id=id;};MyClass.prototype={whoami:function(){return"A Computer"}}constmyInstance=newMyClass(5)console.log(myInstance.whoami())
Now we can create a class for each entry in our thesaurus:
@classEntry @propertyString word="" @propertyArray<String> synonyms= []@endclass
And a class for the thesaurus itself:
@classThesaurus @propertyString name="" @propertyString publisher="" @propertyArray<Any> entries= []@endclass
And we can create a thesaurus!
@newEntry dino("Dino", ["Dinosaur","Raptor","T-Rex"]@newEntry lizard("Lizard", ["Iguana","Gecko","Monitor"]@newEntry paradigm("Paradigm", ["Functional","Imperative","Object Oriented"]@newThesaurus thesaurus("SaurScript Thesaurus","SaurScript Language", [dino, lizard, paradigm])
if myCondition=="yes" { logger.log("Agreed")} elif myCondition!="maybe" { logger.log("So you're telling me there's a chance")}else { logger.log("Not at all")}
SaurScript
doesn't havefor
orwhile
loops. Instead, you can use recursion, orrepeat
.
repeat3 { logger.log("Hi")}
Output:
HiHiHi
We can use recursion to print all of the synonyms in our thesaurus:
@classThesaurus...funallSynonyms(Int index = 0,Array<String> soFar) {if entries[index]!=null { allSynonyms(index+1, arrConcat(soFar, entries[index]) }else { logger.log(soFar) } }@endclass
Sometimes you want to export something for use in another.saur
file.You can add@export
in front of a variable, class, function, etc. to make it available outside the file.
@exportString myName="Carson"@export @class myClass @propertyString greeting="Hello World"@endclass@export @new myClass greeter(strConcat("Hello", myName))@exportfunsayHi(Any greeterClass) > String {return greeterClass.greeting}
It can now be imported from another.saur
file:
@import'myFile'// We can now access everything @exported from the other file:logger.log(sayHi(greeter))
About
Dead-simple functional JavaScript transpiler.
Topics
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.