wip new project example
This commit is contained in:
parent
8c9e574a5e
commit
73e2ddae69
|
@ -6,30 +6,6 @@ _ztl_ is an language transpiler with C/Zig/Rust/Lua/Fortran/Javascript/Elixir st
|
||||||
|
|
||||||
# _ztl_ Grammar and Specification
|
# _ztl_ Grammar and Specification
|
||||||
|
|
||||||
## Trait
|
|
||||||
|
|
||||||
Describes an interface that can be applied to a type, collisions are not important as the names of traits only carry what needs to be implemented a not any implementation itself. for people coming from OOP you can think of traits similar to an abstract class.
|
|
||||||
|
|
||||||
```
|
|
||||||
trait Hashable {
|
|
||||||
fn hash(self) -> u64
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Vec {
|
|
||||||
x: f32,
|
|
||||||
y: f32,
|
|
||||||
z: f32,
|
|
||||||
}
|
|
||||||
|
|
||||||
! maybe the worst hash function ever?
|
|
||||||
impl Hashable Vec {
|
|
||||||
fn hash(self: Vec) -> u64 {
|
|
||||||
return (self.x bxor (self.y sll 16) bxor (self.z sll 32) bor (self.z sll 48))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
## Types
|
## Types
|
||||||
|
|
||||||
- Types can be either structs, enums, or unions.
|
- Types can be either structs, enums, or unions.
|
||||||
|
@ -42,15 +18,7 @@ impl Hashable Vec {
|
||||||
- there are also a list of "substantial types" which come with the language which are the building blocks for more complex types. If you are coming from object oriented languages you can think of self as "primitive types"
|
- there are also a list of "substantial types" which come with the language which are the building blocks for more complex types. If you are coming from object oriented languages you can think of self as "primitive types"
|
||||||
|
|
||||||
```
|
```
|
||||||
struct «type_token» {
|
type «type_token» {
|
||||||
! values
|
|
||||||
}
|
|
||||||
|
|
||||||
enum «type_token» {
|
|
||||||
! values
|
|
||||||
}
|
|
||||||
|
|
||||||
union «type_token» {
|
|
||||||
! values
|
! values
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -111,12 +79,11 @@ Also follows the style boolean 'c' rules of nonzero / zero, but the compiler wil
|
||||||
## null values
|
## null values
|
||||||
|
|
||||||
```
|
```
|
||||||
let a = 3
|
let a:i8 = 3
|
||||||
let b = nil
|
let b:i8
|
||||||
let c = #"nope!"
|
|
||||||
|
|
||||||
let x = a
|
let x:i8 = a
|
||||||
let y = b ?? 1
|
let y:i8 = b ?? 1
|
||||||
|
|
||||||
stdout.write("%d%d\n", x, y) ! outputs 3, 1
|
stdout.write("%d%d\n", x, y) ! outputs 3, 1
|
||||||
```
|
```
|
||||||
|
@ -140,7 +107,7 @@ Much like Lua, zwl only has tables. Lua's tables are amazing and very unique. Wh
|
||||||
syntax (yes I was nice and kept the syntax the same as most C like langs)
|
syntax (yes I was nice and kept the syntax the same as most C like langs)
|
||||||
|
|
||||||
```
|
```
|
||||||
let «variable» = «type»[] ! same as a map of int->«type»
|
let «variable»: «type»[] ! same as a map of int->«type»
|
||||||
|
|
||||||
!or as an array
|
!or as an array
|
||||||
|
|
||||||
|
@ -148,7 +115,7 @@ let «variable» = [val1, val2, ...]
|
||||||
|
|
||||||
! or as a map
|
! or as a map
|
||||||
|
|
||||||
let «variable» = {key1: val1, key2: val2, ...}
|
let «variable»: «type»->«type» = {key1: val1, key2: val2, ...}
|
||||||
```
|
```
|
||||||
|
|
||||||
### tunnel
|
### tunnel
|
||||||
|
@ -331,7 +298,7 @@ in "web mode" the default tunnels are log, info, trace, warn, error, but note th
|
||||||
## Functions
|
## Functions
|
||||||
|
|
||||||
```
|
```
|
||||||
fn «token» («type» «parameter», ...) -> «return_type» {
|
fn «token» («parameter»: «type», ...): «return_type» {
|
||||||
«instructions»
|
«instructions»
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
|
@ -21,7 +21,7 @@
|
||||||
:str 1 check
|
:str 1 check
|
||||||
|
|
||||||
(* do the login here *)
|
(* do the login here *)
|
||||||
} sub
|
} fn
|
||||||
|
|
||||||
255 255 0 :purple color
|
255 255 0 :purple color
|
||||||
|
|
||||||
|
@ -33,15 +33,15 @@
|
||||||
:username str
|
:username str
|
||||||
:password str
|
:password str
|
||||||
|
|
||||||
username 0.0 f32 1.0 f32 2.0 f32 vec purple :me player
|
username 0.0 1.0 2.0 purple :me player
|
||||||
password me login :players set
|
password me login :players set
|
||||||
|
|
||||||
me.pos.x
|
me.pos.x
|
||||||
me.pos.y 10.0 add
|
me.pos.y 10.0 add
|
||||||
me.pos.z 10.0 add vec (* Camera pos *)
|
me.pos.z 10.0 add (* Camera pos *)
|
||||||
me.pos (* Camera looking at point *)
|
me.pos (* Camera looking at point *)
|
||||||
0.0 1.0 0.0 vec (* Camera up vector(rotation towards target) *)
|
0.0 1.0 0.0 (* Camera up vector(rotation towards target) *)
|
||||||
45.0 f32 (* Camera field - of - view Y *)
|
45.0 (* Camera field - of - view Y *)
|
||||||
:CAMERA_PERSPECTIVE (* Camera projection type *)
|
:CAMERA_PERSPECTIVE (* Camera projection type *)
|
||||||
:camera Camera3D
|
:camera Camera3D
|
||||||
|
|
||||||
|
@ -50,7 +50,6 @@
|
||||||
|
|
||||||
(* Main game loop *)
|
(* Main game loop *)
|
||||||
{
|
{
|
||||||
|
|
||||||
:KEY_RIGHT is_key_down {
|
:KEY_RIGHT is_key_down {
|
||||||
0.2 me.pos.x add :me.pos.x set
|
0.2 me.pos.x add :me.pos.x set
|
||||||
true :player_updated set
|
true :player_updated set
|
||||||
|
@ -96,10 +95,10 @@
|
||||||
|
|
||||||
end_drawing
|
end_drawing
|
||||||
}
|
}
|
||||||
{ window_should_close not } while (* Detect window close button or ESC key *)
|
{ window_should_close not } loop-if (* Detect window close button or ESC key *)
|
||||||
|
|
||||||
close_window (*Close window and OpenGL context *)
|
close_window (*Close window and OpenGL context *)
|
||||||
|
|
||||||
} sub
|
} fn
|
||||||
|
|
||||||
main
|
main
|
|
@ -1,43 +1,47 @@
|
||||||
fn build(c: ProjectConfig) {
|
fn build(c: ProjectConfig) {
|
||||||
c.name("MMO Project");
|
c.name("MMO Project");
|
||||||
|
|
||||||
c.client("src/", {
|
c.client([
|
||||||
"c":{
|
LanguageSettings {
|
||||||
"ffi": [
|
lang: "c",
|
||||||
{
|
file: "src/client.ztl",
|
||||||
|
outpath: "client/",
|
||||||
|
ffi: [
|
||||||
|
FFISetting {
|
||||||
"name":"raylib",
|
"name":"raylib",
|
||||||
"library":"$RAYLIB_PATH/libraylib.a",
|
"library":"$RAYLIB_PATH/libraylib.a",
|
||||||
"path":"",
|
"path":"",
|
||||||
"build": "make build",
|
"build": "make build",
|
||||||
}
|
}
|
||||||
],
|
]
|
||||||
"out": "client/"
|
|
||||||
}
|
}
|
||||||
})
|
])
|
||||||
|
|
||||||
c.server("src/",{
|
c.server([
|
||||||
"javascript":{
|
LanguageSettings {
|
||||||
"out":"server/"
|
lang: "javascript",
|
||||||
|
file: "src/server.ztl",
|
||||||
|
outpath: "server/"
|
||||||
}
|
}
|
||||||
});
|
]);
|
||||||
|
|
||||||
c.database("src/",{
|
c.common([
|
||||||
"sqlite":{
|
LanguageSettings {
|
||||||
"out":"db/"
|
lang: "c",
|
||||||
}
|
file: "src/common.ztl",
|
||||||
});
|
outpath: "client/"
|
||||||
|
|
||||||
c.common("src/",{
|
|
||||||
"c":{
|
|
||||||
"out":"server/"
|
|
||||||
},
|
},
|
||||||
"javascript":{
|
LanguageSettings {
|
||||||
"out":"client/"
|
lang: "javascript",
|
||||||
|
file: "src/common.ztl",
|
||||||
|
outpath: "server/"
|
||||||
},
|
},
|
||||||
"sqlite":{
|
LanguageSettings {
|
||||||
"out":"db/"
|
lang: "sqlite",
|
||||||
|
file: "src/common.ztl",
|
||||||
|
outpath: "db/"
|
||||||
}
|
}
|
||||||
});
|
]);
|
||||||
|
|
||||||
c.build();
|
c.build();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,19 +1,19 @@
|
||||||
use "common.ztl"
|
use "common.ztl"
|
||||||
|
|
||||||
fn login(s: tunnel, p: Player, password: str)->Player[] {
|
fn login(s: 9p, p: Player, password: str): []Player {
|
||||||
s.auth(p.username, password);
|
s.auth(p.username, password)
|
||||||
return s.read("players")
|
return s.read("players")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main(argc: int, argv: str[]) {
|
fn main(argc: i32, argv: []str): i32 {
|
||||||
const screen_width: i32 = 800;
|
const screen_width = 800;
|
||||||
const screen_height: i32 = 450;
|
const screen_height = 450;
|
||||||
|
|
||||||
let username = argv[0];
|
let username = argv[0];
|
||||||
let password = argv[1];
|
let password = argv[1];
|
||||||
|
|
||||||
let s = tunnel();
|
let s = tunnel("localhost:25565");
|
||||||
s.attach("localhost:25565");
|
s.attach();
|
||||||
|
|
||||||
let me = Player(
|
let me = Player(
|
||||||
username,
|
username,
|
||||||
|
@ -35,28 +35,28 @@ fn main(argc: int, argv: str[]) {
|
||||||
);
|
);
|
||||||
|
|
||||||
init_window("zwl client : raylib", screen_width, screen_height);
|
init_window("zwl client : raylib", screen_width, screen_height);
|
||||||
set_target_fps(60)
|
set_target_fps(60);
|
||||||
|
|
||||||
(* Main game loop *)
|
(* Main game loop *)
|
||||||
while ( not window_should_close() ) {
|
while ( not window_should_close() ) {
|
||||||
let player_updated = false
|
let player_updated = false;
|
||||||
|
|
||||||
if (is_key_down(:KEY_RIGHT)) {
|
if (is_key_down(KEY_RIGHT)) {
|
||||||
me.pos.x = me.pos.x + 0.2;
|
me.pos.x = me.pos.x + 0.2;
|
||||||
player_updated = true;
|
player_updated = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_key_down(:KEY_LEFT)) {
|
if (is_key_down(KEY_LEFT)) {
|
||||||
me.pos.x = me.pos.x - 0.2;
|
me.pos.x = me.pos.x - 0.2;
|
||||||
player_updated = true;
|
player_updated = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_key_down(:KEY_DOWN)) {
|
if (is_key_down(KEY_DOWN)) {
|
||||||
me.pos.z = me.pos.z + 0.2;
|
me.pos.z = me.pos.z + 0.2;
|
||||||
player_updated = true;
|
player_updated = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_key_down(:KEY_UP)) {
|
if (is_key_down(KEY_UP)) {
|
||||||
me.pos.z = me.pos.z - 0.2;
|
me.pos.z = me.pos.z - 0.2;
|
||||||
player_updated = true;
|
player_updated = true;
|
||||||
}
|
}
|
||||||
|
@ -70,7 +70,7 @@ fn main(argc: int, argv: str[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
begin_drawing();
|
begin_drawing();
|
||||||
clear_background(:RAYWHITE);
|
clear_background(RAYWHITE);
|
||||||
|
|
||||||
begin_mode_3d(camera);
|
begin_mode_3d(camera);
|
||||||
|
|
||||||
|
|
|
@ -1,19 +1,19 @@
|
||||||
type Vec {
|
type Vec {
|
||||||
x: f32,
|
x: f32,
|
||||||
y: f32,
|
y: f32,
|
||||||
z: f32,
|
z: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
type Color {
|
type Color {
|
||||||
r: i8,
|
r: u8,
|
||||||
g: i8,
|
g: u8,
|
||||||
b: i8,
|
b: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
type Player {
|
type Player {
|
||||||
username: str,
|
username: str,
|
||||||
pos: Vec,
|
pos: Vec,
|
||||||
color: Color,
|
color: Color,
|
||||||
}
|
}
|
||||||
|
|
||||||
const purple = Color(255, 255, 0);
|
const purple = Color{255, 255, 0};
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use "common.ztl"
|
use "common.ztl"
|
||||||
|
|
||||||
fn main(argc, argv) {
|
fn main(argc: i32, argv: []str): i32 {
|
||||||
tunnel s();
|
let s = tunnel();
|
||||||
s.host("0.0.0.0:25565",
|
s.host("0.0.0.0:25565",
|
||||||
version,
|
version,
|
||||||
auth,
|
auth,
|
||||||
|
@ -18,42 +18,42 @@ fn main(argc, argv) {
|
||||||
stat);
|
stat);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn version(m) {
|
fn version(m : 9pmsg) {
|
||||||
|
|
||||||
}
|
}
|
||||||
fn auth(m) {
|
fn auth(m : 9pmsg) {
|
||||||
|
|
||||||
}
|
}
|
||||||
fn error(m) {
|
fn error(m : 9pmsg) {
|
||||||
|
|
||||||
}
|
}
|
||||||
fn flush(m) {
|
fn flush(m : 9pmsg) {
|
||||||
|
|
||||||
}
|
}
|
||||||
fn attach(m) {
|
fn attach(m : 9pmsg) {
|
||||||
|
|
||||||
}
|
}
|
||||||
fn walk(m) {
|
fn walk(m : 9pmsg) {
|
||||||
|
|
||||||
}
|
}
|
||||||
fn open(m) {
|
fn open(m : 9pmsg) {
|
||||||
|
|
||||||
}
|
}
|
||||||
fn create(m) {
|
fn create(m : 9pmsg) {
|
||||||
|
|
||||||
}
|
}
|
||||||
fn read(m) {
|
fn read(m : 9pmsg) {
|
||||||
|
|
||||||
}
|
}
|
||||||
fn write(m) {
|
fn write(m : 9pmsg) {
|
||||||
|
|
||||||
}
|
}
|
||||||
fn clunk(m) {
|
fn clunk(m : 9pmsg) {
|
||||||
|
|
||||||
}
|
}
|
||||||
fn remove(m) {
|
fn remove(m : 9pmsg) {
|
||||||
|
|
||||||
}
|
}
|
||||||
fn stat(m) {
|
fn stat(m : 9pmsg) {
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue