g2d

command module
v0.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 15, 2020 License: MPL-2.0 Imports: 8 Imported by: 0

README

Go Report Card     Go Coverage

g2D Programming Language

g2D is an educational programming language conceived to:

  • teach concepts of programming to those who approach the art of coding for the first time
  • quick prototyping canvas rendering 2D contexts - g2D builtins api are really similiar to HTML5 Canvas API
  • having fun with geometry! It's really easy to play with the scripts and viewing the resulting geometries

You can create also animated geometries (thanks to the snapshot builtin function).

g2D is an interpreted language and the interpreter is crafted in Go:

  • single binary file
  • runs on Linux, MacOS and Windows

To execute a g2d-script simply pass the name to the interpreter:

$ g2d ./example/lines.g2d

If no script is passed to the interpreter it will open a REPL shell.

Installation

Do you have go already installed?

$ go get -u github.com/lucasepe/g2d

Ready-To-Use Releases

If you don't want to compile the sourcecode yourself, here you can find the tool already compiled for:

  • MacOS
  • Linux
  • Windows

For those who want to see immediately how the language of g2D feels, here are some examples.

   

   

   

   

The g2D Programming Language Syntax

Example-programs can be found beneath examples/ which demonstrate these things, as well as parts of the standard-library.

Types

g2D has the following data types: null, bool, int, float, str, array, hash, and fn.

Type Syntax Notes
null null
bool true false
int 0 42 1234 -5 is a signed 64-bit integer
float 0.5 4.2 1.234 -5.5 is a 64-bit double-precision floating point
str "" "foo" "\"quotes\" and a\nline break" are immutable arrays of bytes
array [] [1, 2] [1, 2, 3] grow-able arrays (use the push() builtin)
hash {} {"a": 1} {"a": 1, "b": 2} are unordered hash maps
Bindings

Variables are bounded using the := operator.

a := 3
b := 1.2

Variables may be integers, floats, strings, or arrays/hashes.

To update a variable you can simply specify the equals = operator:

a := 3    // Binding
a = a + 5 // Updating
Arithmetic operations

g2D supports all the basic arithmetic operation of int and float types.

a := 5
b := 3

c := a + b
d := c / 2
e := d * d
Builtin containers

cursor contains two builtin containers: array and hash.

Arrays

An array is a list which organizes items by linear sequence. Arrays can hold multiple types.

a := [1, 2.3, "hello!"]
b := [false, true, "Hello World", 3, 3.13]

Adding to an array is done via the push builtin function:

a = append(a, "another")

You can iterate over the contents of an array like so:

i := 0
while( i < len(a) ) {
    print( "Array index ", i, " contains ", a[i], "\n")
    i = i + 1
}

With the definition we included that produces this output:

Array index 0 contains 1
Array index 1 contains 2.3
Array index 2 contains hello!
Array index 3 contains another
Hashes

A hash is a key/value container, but note that keys may only be of type boolean, int and string.

a := {
    "name":"monkey",
    true:1,
    7:"seven",
    "amount": 7.5
}

print(a, "\n")            // Outputs: {name: monkey, true: 1, 7: seven, amount: 7.5}
print(a["amount"], "\n")  // Outputs: 7.4

// Updating...
a[7] = "sette"
print(a[7], "\n")         // Outputs: sette

You can iterate over the keys in a hash via the keys builtin function:

// Fetch all the keys
ids := keys(a)
// ..and iterate
i := 0
while (i < len(ids)) {
    k := ids[i]
    print(i, " Key: ", k, " has Val: ", a[k], "\n")
    i = i + 1
}

You can delete keys via delete (returns an updated value rather than changing it in-place):

// Delete keys via `delete`
a = delete(a, 8)
a = delete(a, "name")
print(a, "\n")           // Outputs: {7: sette, amount: 7.5, true: 1}
Functions

g2D uses fn to define a function which will be assigned to a variable for naming/invocation purposes:

sum := fn(a, b) { return a + b }

print(sum(5,3), "\n")       // Outputs: 8
print(sum(2.5,7.5), "\n")   // Outputs: 10

Functions can be passed as values to others functions:

addTwo := fn(a, b, f) { 
    return 2 + f(a, b) 
}

tot := addTwo(68, 1, sum)
print(tot, "\n")            // Outputs: 71

Functions inside functions

multiplier := fn(q) {
    return fn(x) {
        return x*q
    }
}

multThree := multiplier(3)

print(multThree(2), "\n")  // Outputs: 6
print(multThree(3), "\n")  // Outputs: 9
print(multThree(4), "\n")  // Outputs: 12
If-else statements

g2D supports if-else statements.

max := fn(a, b) {
    if (a > b) {
        return a;
    } else {
        return b;
    }
}

print( max(1, 2) )  // Outputs: 2
Switch Statements

g2D supports the switch and case expressions:

switch n := randi(10) {
    case n % 2 == 0 {
        print(n, " is even", "\n")
    }
    default {
        print(n, " is odd", "\n")
    }
}
While Loops

g2D supports only one looping construct, the while loop:

i := 30
while (i > 0) {
    print(i, " ")
    i = i - 10
}
// 30 20 10

Builtin functions

Core
Function Description
args() returns an array of command-line options passed to the program
exit([status]) exits the program immediately with the optional status or 0
input([prompt] reads a line from standard input optionally printing the specified prompt
print(...) output a string to stdout
printf(pattern, ...) output a string to stdout (formatted according the specified pattern)
sprintf(pattern, ...) like printf(...) but returns a string
bool(val) converts value to a bool
float(val) converts decimal value str to float - if val is invalid returns null
int(val) converts decimal value str to int - if val is invalid returns null
str(val) returns the string representation of val
len(iterable) returns the length of the iterable (string, array or hash)
append(array, val) returns a new array with value pushed onto the end of array
keys(hash) returns the keys of the specified hash
delete(hash, key) returns a new has with the given key deleted
Math
Function Description
abs(x) returns the absolute value of x
sqrt(x) returns the square root of x
hypot(p, q) returns sqrt(p*p + q*q)
pow(x, y) returns x**y, the base x exponential of y
atan(x) returns the arc tangent, in radians, of x
atan2(x, y) returns the arc tangent of y/x
sin(x) returns the sine of the radian argument x
cos(x) returns the cosine of the radian argument x
randf([min], [max]) returns a random float between min and max - by default min=0.0 and max=1.0
randi([min], [max]) returns a random int between min and max
Canvas

The canvas is always square.

Function Description
size([w,h]) when only w is specified creates a squared canvas
when both w and h are specified creates a rectangular canvas
otherwise returns the screen size
worldcoords(xMin, xMax, yMin, yMax) sets up user-defined coordinate system; performs a screen reset (drawings are cleared)
pencolor(hexcolor) sets the pen color to the specified hexcolor; example pencolor("#ff0000")
pencolor(r, g, b, [a]) sets the pen color to r,g,b,a values - should be between 0 and 1, inclusive
pensize(width) sets the pen line thickness to the specified width
stroke([preserve]) strokes the current path with the current color and line width the path is cleared after this operation.
If preserve is true the path will be preserved
fill([preserve]) fills the current path with the current color. Open subpaths are implicity closed.
The path is cleared after this operation. If preserve is true the path is preserved after this operation
circle(x, y, r) draws a circle centered at [x, y] coordinates and with the radius r
ellipse(x, y, rx ,ry) draws an ellipse centered at [x, y] coordinates and with the radii rx and ry
rect(x, y, w, h, [r]) draws a (w x h) rectangle with upper left corner located at (x, y).
If radius r is specified, the rectangle will have rounded corners
moveTo(x, y) starts a new subpath within the current path starting at the specified (x, y) point
lineTo(x, y) adds a line segment to the current path starting at the current point.
If there is no current point, it is equivalent to MoveTo(x, y)
line(x1, y1, x2, y2) draws a line from point (x1, y1) to point (x2, y2)
arc(x, y, r, sa, ea) draws a circular arc centered at (x, y) with a radius of r.
The path starts at sa angle_, ends at ea angle, and travels in the direction given by anticlockwise
ellArc(x, y, rx, ry, sa, ea) draws an elliptical arc centered at (x, y) with a radius of rx in x direction and ry for y direction.
The path starts at sa angle_, ends at ea angle, and travels in the direction given by anticlockwise
text(str, x, y, [ax, ay]) draws the specified text at the specified anchor point
anchor point is x - w * ax, y - h * ay, where w, h is the size of the text
when omitted ax=0.5, ay=0.5 to center the text at the specified point
closePath() adds a line segment from the current point to the beginning of the current subpath.
If there is no current point, this is a no-op.
clearPath() clears the current path. There is no current point after this operation
push() saves the current state of the canvas by pushin it onto a stack
pop() restores the last saved canvas state from the stack
rotate(degrees, [x, y] ) updates the current matrix with a anticlockwise rotation.
If x, y is specified, rotation occurs about this point, otherwise rotation occurs about the origin
snapshot([filename]) creates a PNG image with the current drawings.
If filename is omitted, it will be autogenerated with a progressive counter, that will be incremented on each
snapshot() invocation; this is useful if you wants to generate an animation later (using all the generated PNG images).

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
Package gg provides a simple API for rendering 2D graphics in pure Go.
Package gg provides a simple API for rendering 2D graphics in pure Go.
Package perlin provides coherent noise function over 1, 2 or 3 dimensions This code is go adaptagion based on C implementation that can be found here: http://git.gnome.org/browse/gegl/tree/operations/common/perlin/perlin.c (original copyright Ken Perlin)
Package perlin provides coherent noise function over 1, 2 or 3 dimensions This code is go adaptagion based on C implementation that can be found here: http://git.gnome.org/browse/gegl/tree/operations/common/perlin/perlin.c (original copyright Ken Perlin)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL