Giter Site home page Giter Site logo

cl4cnam / funcsug Goto Github PK

View Code? Open in Web Editor NEW
5.0 2.0 0.0 2.55 MB

An alternative to event-driven programming: a programming language without callbacks to enable you to follow your scenario in GUI programming thanks to additional control flow instructions (structured concurrency)

Home Page: https://github.com/cl4cnam/funcSug/wiki

License: GNU General Public License v3.0

JavaScript 99.27% HTML 0.52% CSS 0.18% Batchfile 0.01% Shell 0.01%
programming-language structured-concurrency interruptible control-flow code-structure gui-programming interactivity no-callbacks task-cancellation callback-hell

funcsug's Introduction

header

FuncSug: An alternative to event-driven programming

FuncSug is a programming language that solves event-driven programming problems: callback hell and state management.

Its goal is mainly facilitating GUI programming (in client-side web programming).

Advantages:

  • It avoids callback hell (You don't need callback any more).
  • Your code follows the order of execution so you avoid spaghetti code and debug more easily.
  • It solves the state management problem (It eliminates the need to manage all the combinations of component states).
  • It easily manages task cancellations (including task timeouts).
  • It can detect a change in the value of a variable and react accordingly.
  • It's deterministic.
  • (You can also include JavaScript snippets, pass FuncSug variables to it and get back JavaScript return).

by means of:

  • explicit logical parallelism,
  • block cancellations,
  • and "await event" instructions.

Tutorials - Getting started - Examples - REPL - Try it online

Please, let me know what you think (Github discussions, Mastodon).
I'd be happy to read your opinion and answer your questions.

solve2_c2

Compare

These two codes do the same thing:

JavaScript FuncSug
let numberOfClick
function $(id) {
   return document.getElementById(id)
}
function launch() {
   $('launch').removeEventListener('click', launch)
   setTimeout(
      ()=>{
         $('clickZone')
         .removeEventListener('click', clickZone)
         console.log("Good job! See you soon!")
      },
      30000
   )
   numberOfClick = 0
   $('clickZone')
   .addEventListener('click', clickZone)
}
let timeoutID
function clickZone() {
   if (numberOfClick == 0) {
      timeoutID = setTimeout(
         ()=>{
            if (numberOfClick < 3) {
               numberOfClick = 0
               console.log("Non-triple click")
            }
         },
         2000
      )
   }
   numberOfClick += 1
   if (numberOfClick == 3) {
      numberOfClick = 0
      console.log("Triple click")
      clearTimeout(timeoutID)
   }
}
$('launch').addEventListener('click', launch)
awaitClickBeep('#launch')
parallel exitAfter 1 finished ||
   waitSeconds(30)
||
   while true:
      awaitClickBeep('#clickZone')
      parallel exitAfter 1 finished ||
         awaitClickBeep('#clickZone')
         awaitClickBeep('#clickZone')
         print("Triple click")
      ||
         waitSeconds(2)
         print("Non-triple click")
print("Good job! See you soon!")

If you copy and paste, replace each three initial spaces by one tabulation (Github markdown doesn't allow to change tabulation size in this context).

Get a taste of the language

Have a look at:

Look at the global structure of the code: One cannot follow this structure in usual programming language.

Let's take, for example, the 'Make Gems' code:

In this language, there is no need to build a ball object, just call a function lifeOfBall. You just run multiple lifeOfBall, birthOfGem and a lifeOfPaddle in parallel. These functions do not build objects: they are just like any classical function.
In the body of lifeOfPaddle, just code the various behaviors of the ball in parallel.

In 'Guess the Number', gameCourse is just a function. The line just after the call of gameCourse is executed only when the player has found the number, that is only when gameCourse has returned, just like any classical function call.

You can also test snippets here.

Concurrent way

logo

FuncSug uses the "concurrent way" or more exactly the "logically parallel way". That is, it introduces explicit concurrency where no concurrency seems present or mandatory. The concurrency is reputed to be very difficult. However, amazingly, many cases (in particular concurrency of waits) are simpler with explicit concurrency!

With this style, you can program (as you program for the console) without users losing the multiple possibilities of interaction (Indeed, this style suppresses the inversion of control of the callback style of event-driven programming).

This "concurrent way" is expressed thanks to additional control flow instructions (beyond if, while, for,...):

  • parallel / parallel exitAfter ... finished / parallel(select ...) / parallel(for ... in ...),
  • select,
  • spawn,
  • whileTrue_dependsOn / whileTrueAwaitFrame_js / whileTrueAwaitDom_js,
  • repeat,
  • sequence,
  • ...

and with new modifiers (beyond break, return): restart, pause, resume.

That enables a better structure of code (that is, a more natural and more readable structure).

If you're curious, you can read the the 'Memory' code or try it or read this post.

It's loosely based on SugarCubes by Jean-Ferdy Susini which is a derivative of Esterel by Gérard Berry. It adheres to "structured concurrency" principles. It doesn't use OS threads. It doesn't aim to improve speed of execution.

Note

Goal: facilitating GUI programming (in fact, the interactivity aspect)
Non-goals: improving speed of execution, facilitating data structuration, object management, mutability, types

Instructions for use

In your myProgram.html, in the end of the body element, include the lines:

  <script src="https://cdn.jsdelivr.net/gh/cl4cnam/funcSug/libStd.fg" type="application/funcsug"></script>
  <script src="https://cdn.jsdelivr.net/gh/cl4cnam/funcSug/libDOM.fg" type="application/funcsug"></script>
  <script src="https://cdn.jsdelivr.net/gh/cl4cnam/funcSug/libDOMSVG.fg" type="application/funcsug"></script>
  <script src="myProgram.fg" type="application/funcsug"></script>
  <script src="https://cdn.jsdelivr.net/gh/cl4cnam/funcSug/parser.js"></script>
  <script src="https://cdn.jsdelivr.net/gh/cl4cnam/funcSug/parserPy.js"></script>
  <script src="https://cdn.jsdelivr.net/gh/cl4cnam/funcSug/interpreter.js"></script>
  <script src="https://cdn.jsdelivr.net/gh/cl4cnam/funcSug/DOMloader.js"></script>

Write your code in the file myProgram.fg.

Specific case: With phaser_ce

In your myProgram.html, in the end of the body element, include the lines:

  <script src="https://cdn.jsdelivr.net/gh/photonstorm/phaser-ce/build/phaser.min.js"></script>
  <script src="https://cdn.jsdelivr.net/gh/cl4cnam/funcSug/libStd.fg" type="application/funcsug"></script>
  <script src="https://cdn.jsdelivr.net/gh/cl4cnam/funcSug/libDOM.fg" type="application/funcsug"></script>
  <script src="https://cdn.jsdelivr.net/gh/cl4cnam/funcSug/libPhaser.fg" type="application/funcsug"></script>
  <script src="myProgram.fg" type="application/funcsug"></script>
  <script src="https://cdn.jsdelivr.net/gh/cl4cnam/funcSug/parser.js"></script>
  <script src="https://cdn.jsdelivr.net/gh/cl4cnam/funcSug/parserPy.js"></script>
  <script src="https://cdn.jsdelivr.net/gh/cl4cnam/funcSug/interpreter.js"></script>
  <script src="https://cdn.jsdelivr.net/gh/cl4cnam/funcSug/DOMloader.js"></script>

Write your code in the file myProgram.fg.

Some features

Parallel construct

Beware that this is just a logical parallelism.

You can write

parallel ||
	<parallelBranch1>
||
	<parallelBranch2>

to execute parallelBranch1 and parallelBranch2 concurrently.

You can also:

  • add a branch dynamically,
  • select parallel branch(es) according to event(s).

Interruptible block

You can interrupt (break), pause (pause), resume (resume) or restart (restart) a block. For example:

parallel ||
	@myBlock
	<instruction1>
	<instruction2>
||
	break myBlock

Reaction to a change of a variable

You can react to a change of a variable, for example, like this:

while true:
	awaitBeep myVariable
	<whatToDoInThisCase>

Syntax elements

The syntax is very similar to that of Python.

After the colon : of some blocks, you can add @label just after the function name to label the expression (This is useful for the break instruction). This 'label' must be a declared variable.

A few instructions/expressions (loose description)

print(value) writes value onto the console.

var variableName declares a new local variable.

variableName := value assigns value to variableName.

variableName += value increments variableName by value (also for -,*,/ operators).

true and false returns the boolean value 'true' and 'false' respectively.

value1 + value2 returns the sum/concatenation of the two values (also for -,*,/,mod).

value1 < value2 returns the truth value of value1 < value2 (also for <=,=,/=, and, or).

randomIntBetween(value1, value2) returns a random integer between value1 and value2.

if, while, def acts as usual in Python.

break label interrupts the execution of the expression labelled by label.

seq:
	expression1
	...
	expressionN

executes the expressions in sequence and returns the value of the last.

par:
	expression1
	...
	expressionN

executes the expressions in parallel.

onBreak:
	expression

executes expression if the preceding expression is interrupted.

js (variable1, ..., variableN):
	jsString

executes the code in jsString (in which variable1 ... variableN can be used).

displayNewMessageIn(text cssSelector) displays a new message text into the DOM element identified by cssSelector.

displayNewMessageIn(text cssSelector/cssClass) displays a new message text into the DOM element identified by cssSelector and assigns cssClass to it.

awaitNewHumanNumberIn(cssSelector) awaits a number from user in the DOM element identified by cssSelector.

awaitClickBeep(cssSelector) awaits a click from user in the DOM element identified by cssSelector.

Contributing

Pull requests are welcome.

If you want to make big change, please open an issue first to discuss it.

You can just post your thoughts into discussions tab too.

Examples of contributions: Tutorials, optimization, translations, fixing bugs, ...

Various

This language has syntax highlighting for Geany; otherwise, with Geany, if you choose that of zephir (or nsis, powershell, ruby), it can be pleasant.

The file parserPy.js has been generated online from the file funcSugPy.peggyjs on the site https://peggyjs.org/online.html with "parser variable" set to "pegPy".

For now, this implementation consists just in a quick and dirty interpreter but it works enough to be appreciated.

Work in progress.

funcsug's People

Contributors

cl4cnam avatar vbatcnam avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  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.