Giter Club home page Giter Club logo

hap's Introduction

Hap

Hap is a dynamically typed, asynchronous, imperative programming language.

This is the original C++ prototype, which is no longer maintained; it’s superseded by the new Haskell implementation of Hap, which has an improved performance model and a graphical mode.

Here is a simple example of Hap’s syntax and semantics:

var health = 100;

when (health <= 0) {
  print("Goodbye, cruel world!");
  exit;
}

print("Hello, sweet world!");

while (true)
  health = health - 1;

Hap code reads much like code in other imperative languages. Its syntax is reminiscent of JavaScript. However, note the use of when: this is an asynchronous conditional. The above code prints:

Hello, sweet world!
Goodbye, cruel world!

Synchronous flow control statements such as if, while, and for are evaluated immediately. If the condition is true when the statement is reached, then the body is evaluated immediately, before proceeding to subsequent statements.

Asynchronous flow control statements such as when are evaluated concurrently. The flow of control proceeds to subsequent statements immediately; only when the conditional actually becomes true is the body evaluated.

This allows programs to be structured implicitly in terms of events and time-based constraints. You are not forced to subscribe to event handlers for predefined conditions—rather, you can create ad-hoc conditions as needed, and this is syntactically lightweight.

Types

TypeExamples
int
0
1234
bool
false
true
text
""
"scare"
list
[]

[1, 2, 3, 4, 5]

[ "this", "that", "and", "the", "other", "thing", ]

[ [ +cos(x), -sin(x) ], [ +sin(x), +cos(x) ] ]

[0, false, ""]

map
{}

{ "one": 1, "two": 2, "three": 3 }

[ { en: "one", fr: "un", }, { en: "two", fr: "deux", }, { en: "three", fr: "trois", }, ]

Statements

General

StatementDescription
var NAME;
var NAME = EXPR;
Creates a lexically scoped variable named NAME. Initializes it to EXPR, or gives it an initial value of undef if EXPR is not specified.
fun NAME(PARAM, PARAM, ...) STAT
Creates a lexically scoped function named NAME with a body given by STAT using the given parameters.
EXPR;
Synchronous. Evaluates EXPR and discards the result.
{ STAT... }
Synchronous. Evaluates a block of statements as a unit. Introduces a new lexical scope.
exit
Exits the program entirely.
last
Exits the current loop.
next
Jumps to the next iteration of the current loop, re-evaluating the loop condition.
redo
Redoes the current iteration of the current loop, without re-evaluating the loop condition (or step, in the case of for).

Flow Control

Statement Synchronicity How many times STAT is evaluated When STAT is evaluated
if EXPR STAT
Synchronous Once If EXPR is true when the statement is reached.
when EXPR STAT
Asynchronous Once The first time EXPR becomes true; immediately if EXPR is already true.
whenever EXPR STAT
Asynchronous Once Every time EXPR becomes true; immediately if EXPR is already true.
while EXPR STAT
Synchronous Repeatedly As long as EXPR remains true; never if EXPR is already false.
for (INIT; COND; STEP) STAT
Synchronous Repeatedly

Equivalent to:

INIT;
while (COND) {
  STAT;
  STEP;
}

Except that variables declared in INIT are local to the loop, and STEP is evaluated even when the next statement is used.

hap's People

Contributors

evincarofautumn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

hap's Issues

Pointers want to be functors.

I often want to do this:

pointer<T> x = ...;
return x ? f(x) : pointer();

Which is really just a functor, and should be expressible as:

pointer<T> x = ...;
return lift(f, x);

Using accept_statement

I was thinking that function body and 'For' body should use accept_statements because of block statements. But I noticed from test that accept_statement worked, could you explain?

shared_ptr body(accept_statement(environment));

Add game-related features.

Since Hap is geared toward game prototyping, I think it makes sense to ship with a default graphics/audio/input backend, probably based on SDL2.

We can easily translate SDL events into Hap events:

when (mouse.left.clicked) {
    …
}

And having access to such things as SDL user-defined events and timers as built-in types seems worthwhile:

when (every(0.5)) {
    …
}

Where every(x) is a switch that becomes true every x seconds (within timer resolution).

The API for all this still needs to be designed.

Removing Tokens

Thinking of changing accept(Token(Token::IDENTIFIER, "atomic")))
to accept(Token::ATOMIC), and making tokenize to recognize it too. Will it be too much?

This could be repeated for some statements.

Switch to push-based semantics.

In the current semantics, listeners are pull-based: at every sequence point, they poll their conditions to see whether they’ve become true. This is wasteful, because we evaluate the expression for all listeners, even though most of them will not have changed.

I’ve written a proof of concept in C# with a push-based semantics. All expressions keep backreferences (in the form of weak pointers) to the expressions that depend on them, so only those conditions whose values have changed need to be re-tested. This involves some tedious management of the backreferences to ensure that dependencies are correctly tracked, but I think it could be ported over to C++ easily enough.

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.