Skip to content

Instantly share code, notes, and snippets.

@norman784
Last active November 22, 2019 10:41
Show Gist options
  • Save norman784/cf2d9713492cb9613c92bf156224ff1a to your computer and use it in GitHub Desktop.
Save norman784/cf2d9713492cb9613c92bf156224ff1a to your computer and use it in GitHub Desktop.
Syntax exploration for programming language focused on game development prototyping
-- Pseudo oversimplistic snake implementation to explore language syntax
-- Import multiple elements of the same package
import std.input { Key key_pressed }
-- Struct declaration
struct Canvas { height: Float width: Float }
struct Vec2 { x: Float y: Float }
struct Snake {
cells: Vec2[]
dx: Float
dy: Float
max_cells: Integer
x: Float
-- The >>> tells the compiler/interpreter to rename the property, it is only usefull when using the ecs database
-- when the compiler/interpreter told the ecs database it renames automatically all the occurrences in the files
-- and remove the {old field} >>> {new field} in favor of {new field}.
-- In resume the syntax is {old name} >>> {new name}, but this only works on strucs.
_y >>> y: Float
}
-- Constant declaration
grid = 16.0;
-- Function declaration
calculate_position position size = {
if position < 0 {
size - grid;
} else if position > size {
0; -- is equivalent of `return 0`
} else {
position;
}
}
-- Destructuring data param properties
calculate_direction { dx dy x y }: Snake = Vec2 {
x = x + dx,
y = y + dy,
}
calculate_x { x }: Snake { width }: Canvas = calculate_position x width;
calculate_y { y }: Snake { height }: Canvas = calculate_position y height;
-- Always last expression is returned
calculate_cells { cells, max_cells }: Snake = {
if cells.length > max_cells {
cells.slice (cell.length - 1) 1;
} else {
cells;
}
}
calculate_direction { dx dy }: Snake = {
-- We might add parentheses to a function call to prevent
-- the parser to pass other statements as parameter of the function
if (key_pressed Key.ArrowLeft) && dx == 0 {
-- Returns a tuple or array
[ -grid, 0.0 ]
} else if (key_pressed Key.ArrowUp) && dy == 0 {
[ 0.0, -grid ]
if (key_pressed Key.ArrowRight) && dx == 0 {
[ grid, 0.0 ]
} else if (key_pressed Key.ArrowDown) && dy == 0 {
[ 0.0, grid ]
} else {
[ dx, dy ]
}
}
loop canvas: Canvas snake: Snake = {
-- Can also be expressed like this `calculate_x snake canvas`
x = snake.calculate_x canvas;
y = snake.calculate_y canvas;
-- Destructuring tuples or arrays
[ dx, dy ] = snake.calculate_direction;
cells = snake.calculate_cells;
-- Destructuring data properties
loop canvas {...snake, cells, dx, dy, x, y, };
}
main = {
-- Instantiate data
canvas = Canvas {
height = 600.0,
width = 800.0,
}
snake = Snake {
cells = [],
dx = grid,
dy = 0.0,
max_cells = 4,
x = 400.0,
y = 300.0,
}
-- Function call
loop canvas snake;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment