Skip to content

Commit

Permalink
feat: Renamed const keyword to final
Browse files Browse the repository at this point in the history
closes #318
  • Loading branch information
giann committed Nov 28, 2024
1 parent 6195ed4 commit 418ba84
Show file tree
Hide file tree
Showing 92 changed files with 380 additions and 380 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- Qualified name now use `\` separator instead of `.`
- The `float` type is renamed to `double` (https://github.com/buzz-language/buzz/issues/311)
- Enum type is declared between `<>` rather than `()`
- `const` keyword is renamed to `final` (https://github.com/buzz-language/buzz/issues/318)

## Added
- Object can have `const` properties (https://github.com/buzz-language/buzz/issues/13). A object with only `const` properties is considered itself `const`. Although we don't do anything yet with that concept. https://github.com/buzz-language/buzz/issues/114 is the objective but it requires being able to build objects and instances at compile time which is not yet possible.
Expand Down
12 changes: 6 additions & 6 deletions examples/2048.buzz
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ object Game {
invalidMove: bool = false,

static fun init(size: int = 4, goal: int = 2048) > Game {
const game = Game{
final game = Game{
size,
goal
};
Expand Down Expand Up @@ -76,13 +76,13 @@ object Game {

// Add new tile at a random free sp
fun addNewTile() > void {
const free = this.freeSpots();
final free = this.freeSpots();

if (free.len() == 0) {
return;
}

const spot = free[std\random(min: 0, max: free.len() - 1)];
final spot = free[std\random(min: 0, max: free.len() - 1)];
this.board[spot.x][spot.y] = if (std\random(min: 0, max: 9) == 0)
4
else
Expand Down Expand Up @@ -158,7 +158,7 @@ object Game {

foreach (x in 0..this.size) {
foreach (y in 0..this.size) {
const pair = Pair{
final pair = Pair{
srcX = x,
srcY = y,
srcValue = this.board[x][y],
Expand Down Expand Up @@ -227,7 +227,7 @@ object Game {

fun move() > bool {
io\stdout.write("(wasd to move, q to quit, r to restart)\n> ") catch void;
const command = io\stdin.read(1) catch void;
final command = io\stdin.read(1) catch void;

if (command == "q") {
os\exit(0);
Expand Down Expand Up @@ -294,7 +294,7 @@ object Game {
}

fun main(_: [str]) > void {
const game = Game.init();
final game = Game.init();

game.addNewTile();
game.render();
Expand Down
12 changes: 6 additions & 6 deletions examples/brownian-tree.buzz
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ fun setParticle(canvas: [[bool]]) > obj{ x: int, y: int } {

fun iterate(canvas: [[bool]], numParticles: int) {
foreach (_ in 0..numParticles) {
const pos = setParticle(canvas);
final pos = setParticle(canvas);

while (true) {
const dx = std\random(min: 0, max: 5) - 3;
const dy = std\random(min: 0, max: 5) - 3;
final dx = std\random(min: 0, max: 5) - 3;
final dy = std\random(min: 0, max: 5) - 3;

if (pos.x + dx >= 0 and pos.x + dx < canvas.len() and pos.y + dy >= 0 and pos.y + dy < canvas[0].len()) {
if (canvas[pos.x + dx][pos.y + dy]) {
Expand Down Expand Up @@ -72,9 +72,9 @@ fun draw(canvas: [[bool]], renderer: Renderer) > void !> SDLError {
}

fun main(_: [str]) > void !> SDLError {
const sizeX = 200;
const sizeY = 200;
const numParticles = 16000;
final sizeX = 200;
final sizeY = 200;
final numParticles = 16000;

var canvas: [[bool]] = [];
foreach (i in 0..sizeX) {
Expand Down
2 changes: 1 addition & 1 deletion examples/fibonacci.buzz
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fun fibonacci(n: int) > void *> int? {
}

fun main(args: [str]) > void {
const N = std\parseInt(args[?0] ?? "10") ?? 10;
final N = std\parseInt(args[?0] ?? "10") ?? 10;

foreach (n in &fibonacci(N)) {
std\print("{n}");
Expand Down
6 changes: 3 additions & 3 deletions examples/fizzbuzz.buzz
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ namespace examples\fizzbuzz;
import "std";

fun fizzBuzz(n: int) > str {
const fizz = n % 3 == 0;
const buzz = n % 5 == 0;
final fizz = n % 3 == 0;
final buzz = n % 5 == 0;

if (fizz and buzz) {
return "FizzBuzz";
Expand All @@ -18,7 +18,7 @@ fun fizzBuzz(n: int) > str {
}

fun main(args: [str]) > void {
const limit = std\parseInt(args[?0] ?? "10") ?? 10;
final limit = std\parseInt(args[?0] ?? "10") ?? 10;

foreach (n in 0..limit) {
std\print("{n}: {fizzBuzz(n)}");
Expand Down
12 changes: 6 additions & 6 deletions examples/game-of-life.buzz
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ object World {
var cells = [<bool>];
for (y: int = 0; y < this.height; y = y + 1) {
for (x: int = 0; x < this.width; x = x + 1) {
const coordinate = y * this.width + x;
const liveNeighbors = this.neighbors(x: x, y: y);
final coordinate = y * this.width + x;
final liveNeighbors = this.neighbors(x: x, y: y);

cells.append(liveNeighbors == 3 or (this.cells[coordinate] and liveNeighbors == 2));
}
Expand All @@ -64,8 +64,8 @@ object World {
}

fun draw(renderer: Renderer) > void !> SDLError {
const cellWidth = 800 / this.width;
const cellHeight = 600 / this.height;
final cellWidth = 800 / this.width;
final cellHeight = 600 / this.height;

for (y: int = 0; y < this.height; y = y + 1) {
for (x: int = 0; x < this.width; x = x + 1) {
Expand Down Expand Up @@ -154,8 +154,8 @@ fun main(args: [str]) > void !> SDLError {
height: 600,
);

const width = if (args.len() > 0) std\parseInt(args[0]) else null;
const height = if (args.len() > 1) std\parseInt(args[1]) else null;
final width = if (args.len() > 0) std\parseInt(args[0]) else null;
final height = if (args.len() > 1) std\parseInt(args[1]) else null;

var world = World.init(
width: width ?? 10,
Expand Down
16 changes: 8 additions & 8 deletions examples/sdl-wrapped.buzz
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ export object Renderer {
width: int,
height: int
) > Texture !> SDLError {
const texture = SDL_CreateTexture(
final texture = SDL_CreateTexture(
this.renderer,
format: std\toDouble(format.value),
access: access.value,
Expand All @@ -314,7 +314,7 @@ export object Renderer {
}

fun setRenderTarget(texture: Texture?) > void !> SDLError {
const result = SDL_SetRenderTarget(
final result = SDL_SetRenderTarget(
this.renderer,
texture: texture?.texture
);
Expand All @@ -327,7 +327,7 @@ export object Renderer {
}

fun setRenderDrawColor(color: Color) > void !> SDLError {
const result = SDL_SetRenderDrawColor(
final result = SDL_SetRenderDrawColor(
this.renderer,
r: color.red,
g: color.green,
Expand All @@ -343,7 +343,7 @@ export object Renderer {
}

fun fillRect(rect: Rect) > void !> SDLError {
const result = SDL_RenderFillRect(
final result = SDL_RenderFillRect(
this.renderer,
rect: SDL_Rect{
x = rect.x,
Expand All @@ -361,7 +361,7 @@ export object Renderer {
}

fun drawPoint(x: int, y: int) > void !> SDLError {
const result = SDL_RenderDrawPoint(
final result = SDL_RenderDrawPoint(
this.renderer,
x, y,
);
Expand All @@ -374,7 +374,7 @@ export object Renderer {
}

fun renderCopy(texture: Texture, srcRect: Rect? = null, dstRect: Rect? = null) > void !> SDLError {
const result = SDL_RenderCopy(
final result = SDL_RenderCopy(
this.renderer,
texture: texture.texture,
srcrect: if (srcRect -> rect)
Expand Down Expand Up @@ -427,7 +427,7 @@ export object Window {
windowFlags = windowFlags | flag.value;
}

const window = SDL_CreateWindow(
final window = SDL_CreateWindow(
ffi\cstr(name),
x,
y,
Expand Down Expand Up @@ -456,7 +456,7 @@ export object Window {
rendererFlags = rendererFlags | flag.value;
}

const renderer = SDL_CreateRenderer(
final renderer = SDL_CreateRenderer(
this.window,
index: index,
flags: std\toDouble(rendererFlags),
Expand Down
8 changes: 4 additions & 4 deletions examples/sdl.buzz
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ import "examples/sdl-wrapped" as _;

// buzz -L/path/to/SDL2.dylib/so/dll examples/sdl.buzz
fun main(_: [str]) > int !> SDLError {
const sdl = SDL.init([Subsystem.Video]);
final sdl = SDL.init([Subsystem.Video]);

const window = Window.init(
final window = Window.init(
"SDL FFI test",
width: 800,
height: 600,
flags: [WindowFlag.OpenGL, WindowFlag.AllowHighDPI],
);

const renderer = window.createRenderer(
final renderer = window.createRenderer(
index: -1,
flags: [RendererFlag.Accelerated, RendererFlag.TargetTexture],
);

const texture = renderer.createTexture(
final texture = renderer.createTexture(
format: PixelFormat.RGBA8888,
access: TextureAccess.Target,
width: 800,
Expand Down
28 changes: 14 additions & 14 deletions examples/sqlite.buzz
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ enum<int> ResultCode {
Empty = 16, // Internal use only
Schema = 17, // The database schema changed
TooBig = 18, // String or BLOB exceeds size limit
Constraint = 19, // Abort due to constraint violation
Constraint = 19, // Abort due to finalraint violation
Mismatch = 20, // Data type mismatch
Misuse = 21, // Library used incorrectly
NoLfs = 22, // Uses OS features not supported on host
Expand Down Expand Up @@ -156,7 +156,7 @@ export object Statement {
std\print("Executing query `{this.query}`");
var code: ResultCode = ResultCode.Ok;

const rows = [<[Boxed]>];
final rows = [<[Boxed]>];
while (code != ResultCode.Done) {
code = ResultCode(sqlite3_step(this.stmt)) ?? ResultCode.Error;
if (code != ResultCode.Ok and code != ResultCode.Row and code != ResultCode.Done) {
Expand All @@ -170,10 +170,10 @@ export object Statement {
break;
}

const columnCount = sqlite3_column_count(this.stmt);
final columnCount = sqlite3_column_count(this.stmt);
var row = [<any>];
for (i: int = 0; i < columnCount; i = i + 1) {
const columnType = Type(sqlite3_column_type(this.stmt, col: i)) ?? Type.Null;
final columnType = Type(sqlite3_column_type(this.stmt, col: i)) ?? Type.Null;

if (columnType == Type.Integer) {
row.append(
Expand All @@ -196,7 +196,7 @@ export object Statement {
}
}

const boxedRow = (Boxed.init(row) catch void).listValue();
final boxedRow = (Boxed.init(row) catch void).listValue();
rows.append(boxedRow);

_ = yield boxedRow;
Expand All @@ -222,10 +222,10 @@ export object Database {
}

fun prepareRaw(query: str) > Statement !> SQLiteError {
const buffer = buffer\Buffer.init();
final buffer = buffer\Buffer.init();
buffer.writeUserData(std\toUd(0)) catch void;

const code = ResultCode(
final code = ResultCode(
sqlite3_prepare_v2(
db: this.db,
zSql: ffi\cstr(query),
Expand All @@ -251,7 +251,7 @@ export object Database {

export object SQLite {
static fun init() > SQLite !> SQLiteError {
const code = ResultCode(sqlite3_initialize()) ?? ResultCode.Error;
final code = ResultCode(sqlite3_initialize()) ?? ResultCode.Error;
if (code != ResultCode.Ok) {
throw SQLiteError{
code = code,
Expand All @@ -276,10 +276,10 @@ export object SQLite {
cFlags = cFlags | flag.value;
}

const buffer = buffer\Buffer.init();
final buffer = buffer\Buffer.init();
buffer.writeUserData(std\toUd(0)) catch void;

const code = ResultCode(
final code = ResultCode(
sqlite3_open_v2(
filename: ffi\cstr(filename),
ppDb: buffer.ptr(),
Expand All @@ -304,11 +304,11 @@ export object SQLite {
}

test "Testing sqlite" {
const sqlite = SQLite.init();
final sqlite = SQLite.init();

const database = sqlite.open("./test.db", flags: [OpenFlag.ReadWrite, OpenFlag.Create]);
final database = sqlite.open("./test.db", flags: [OpenFlag.ReadWrite, OpenFlag.Create]);

const statement = database.prepareRaw(`
final statement = database.prepareRaw(`
CREATE TABLE IF NOT EXISTS test (
name TEXT NOT NULL
)
Expand All @@ -323,7 +323,7 @@ test "Testing sqlite" {
.execute(database);
}

const select = Query{}
final select = Query{}
.select([ "rowid", "name" ])
.@"from"("test")
.prepare(database);
Expand Down
8 changes: 4 additions & 4 deletions examples/voronoi-diagram.buzz
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fun generateVoronoi(renderer: Renderer, width: int, height: int, numCells: int)
var dmin = hypot(x: width - 1, y: height - 1);
var j = -1;
foreach (i in 0..numCells) {
const d = hypot(x: nx[i] - x, y: ny[i] - y);
final d = hypot(x: nx[i] - x, y: ny[i] - y);
if (d < dmin) {
dmin = d;
j = i;
Expand All @@ -67,9 +67,9 @@ fun generateVoronoi(renderer: Renderer, width: int, height: int, numCells: int)
}

fun main(_: [str]) > void !> SDLError {
const width = 400;
const height = 400;
const numCells = 50;
final width = 400;
final height = 400;
final numCells = 50;

var sdl = SDL.init([Subsystem.Video]);

Expand Down
4 changes: 2 additions & 2 deletions src/Ast.zig
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ pub const NamedVariable = struct {
value: ?Node.Index,
slot: Slot,
slot_type: SlotType,
slot_constant: bool,
slot_final: bool,
};

pub const ObjectDeclaration = struct {
Expand Down Expand Up @@ -698,7 +698,7 @@ pub const VarDeclaration = struct {
name: TokenIndex,
value: ?Node.Index,
type: ?Node.Index,
constant: bool,
final: bool,
slot: Slot,
slot_type: SlotType,
};
Expand Down
1 change: 1 addition & 0 deletions src/Chunk.zig
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ pub fn HashMap(V: type) type {
allocator: std.mem.Allocator,
/// AST
ast: Ast,
// TODO: merge `code` and `lines` in a multiarray
/// List of opcodes to execute
code: std.ArrayListUnmanaged(u32),
/// List of locations
Expand Down
Loading

0 comments on commit 418ba84

Please sign in to comment.