Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Dead-simple functional JavaScript transpiler.

NotificationsYou must be signed in to change notification settings

SaurScript/saurscript-lang

Repository files navigation

SaurScript BannerDead-simple functionalJavaScript 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)

Features

  • Functional
  • Pure
  • Statically typed
  • Lazy
  • Easy to learn

Concept

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.

LearnSaurScript

To learn how to useSaurScript we're going to create a thesaurus.

Imports

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

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 ofStrings (synonyms).

Functions

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

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, Else If, and Else

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")}

Repeat

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

Exports

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

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp