Skip to content

Commit

Permalink
using mem_num
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard authored and Richard committed Mar 30, 2019
1 parent 2a1a635 commit f818701
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 67 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wasp"
version = "0.4.0"
version = "0.4.1"
authors = ["Richard Anaya"]
edition = "2018"
license = "MIT"
Expand All @@ -9,7 +9,6 @@ description = "a web assembly lisp programming language"
[dependencies]
failure = "0.1.5"
wasmly = "0.2.0"
byteorder = "1.3.1"
clap = "2"
walkdir = "2"

Expand Down
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Using [web-dom](https://github.com/web-dom/web-dom) we can easily draw something
ctx (htmlcanvas_get_context canvas "2d")]
(loop [x 0]
(if (< x 3)
(do (drawing_set_fill_style ctx (mem32 (+ colors (* 4 x))))
(do (drawing_set_fill_style ctx (mem_num (+ colors (* 8 x))))
(drawing_fill_rect ctx (* x 10) (* x 10) 50 50 )
(recur [x (+ x 1)]))))))
```
Expand All @@ -103,7 +103,7 @@ It's often important for a web assembly modules to have some sort of global data

(defn run_my_game
...
(mem32 high_score (+ (mem32 high_score) 100)
(mem_num high_score (+ (mem_num high_score) 100)
...)
```

Expand Down Expand Up @@ -149,10 +149,8 @@ Please try to use non conflicting names in meantime while this is fleshed out.
* **(function_name ...)** - call a function with arguments
* **(mem x:integer)** - get 8-bit value from memory location x
* **(mem x:integer y)** - set 8-bit value at memory location x to value y
* **(mem32 x:integer)** - get 32-bit integer value from memory location x
* **(mem32 x:integer y)** - set 32-bit integer value at memory location x to value y
* **(memf64 x:integer)** - get 64-bit float value from memory location x
* **(memf64 x:integer y)** - set 64-bit float value at memory location x to value y
* **(mem_num x:integer)** - get 64-bit float value from memory location x
* **(mem_num x:integer y)** - set 64-bit float value at memory location x to value y

* **(mem_heap_start)** - get number that represents the start of the heap
* **(mem_heap_end)** - get number that represents the end of the heap
Expand Down
Binary file modified compiler/compiler.wasm
Binary file not shown.
Binary file modified examples/canvas/canvas.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion examples/canvas/main.w
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
ctx (htmlcanvas_get_context canvas "2d")]
(loop [x 0]
(if (< x 3)
(do (canvas_set_fill_style ctx (mem32 (+ colors (* 4 x))))
(do (canvas_set_fill_style ctx (mem_num (+ colors (* 8 x))))
(canvas_fill_rect ctx (* x 10) (* x 10) 50 50 )
(recur [x (+ x 1)]))))))
1 change: 0 additions & 1 deletion examples/simplest/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@
})
.then(results => {
console.log(results.instance.exports.main(42));
debugger;
});
</script>
5 changes: 5 additions & 0 deletions examples/webgl/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
build:
cd ../../ && cargo build
../../target/debug/wasp build
serve:
http-server -p 8080
12 changes: 12 additions & 0 deletions examples/webgl/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Wasp</title>
<script src="https://unpkg.com/@webcomponents/webcomponentsjs@latest/webcomponents-loader.js"></script>
<script src="https://unpkg.com/web-dom@latest/web-dom.js"></script>
</head>
<body>
<canvas id="screen" width="500" height="500"></canvas>
<web-dom module="webgl.wasm"></web-dom>
</body>
</html>
115 changes: 115 additions & 0 deletions examples/webgl/main.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
(def COLOR_BUFFER_BIT 16384)
(def VERTEX_SHADER 35633)
(def FRAGMENT_SHADER 35632)
(def vertex_shader_src "
precision mediump float;
attribute vec2 vertPosition;
attribute vec3 vertColor;
varying vec3 fragColor;
void main()
{
fragColor = vertColor;
gl_Position = vec4(vertPosition, 0.0, 1.0);
}
")
(def fragment_shader_src "
precision mediump float;

varying vec3 fragColor;
void main()
{
gl_FragColor = vec4(fragColor, 1.0);
}
")

(extern global_get_window [])
(extern window_get_document [window])
(extern document_query_selector [document query])
(extern htmlcanvas_get_context [element context])
(extern webgl_create_shader [ctx shader_type] )
(extern webgl_shader_source [ctx shader source] )
(extern webgl_compile_shader [ctx shader] )
(extern webgl_create_shader [ctx shader_type vertex_shader_src] )
(extern webgl_create_program [ctx] )
(extern webgl_attach_shader [ctx program shader] )
(extern webgl_link_program [ctx program] )
(extern webgl_use_program [ctx program] )
(extern webgl_clear_color [ctx r g b a] )
(extern webgl_clear [ctx buffer_bit] )
(extern webgl_get_attrib_location [ctx program attrib_name] )

(defn create_shader [ctx shader_type source]
(let [shader (webgl_create_shader ctx shader_type)]
(webgl_shader_source ctx shader source)
(webgl_compile_shader ctx shader)
shader))

(defn start_program [ctx]
(let [vertex_shader (create_shader ctx VERTEX_SHADER vertex_shader_src)
fragment_shader (create_shader ctx FRAGMENT_SHADER fragment_shader_src)
program (webgl_create_program ctx)]
(webgl_attach_shader ctx program vertex_shader)
(webgl_attach_shader ctx program fragment_shader)
(webgl_link_program ctx program)
(webgl_use_program ctx program)
program))

(pub defn main []
(let [win (global_get_window)
doc (window_get_document win)
canvas (document_query_selector doc "#screen")
ctx (htmlcanvas_get_context canvas "webgl")]
(webgl_clear_color ctx 0.75 0.85 0.8 1.0)
(webgl_clear ctx COLOR_BUFFER_BIT)
(let [program (start_program ctx)
position_location (webgl_get_attrib_location ctx program "vertPosition")
color_location (webgl_get_attrib_location ctx program "vertColor")]
123)))


; // create a program and get its attribute and uniforms
; let program = start_program(ctx);
; let position_location = webgl::get_attrib_location(ctx, program, "vertPosition");
; let color_location = webgl::get_attrib_location(ctx, program, "vertColor");
; webgl::use_program(ctx, NULL);
;
; // setup data buffer
; let vertices: Vec<f32> = vec![
; // X, Y, R, G, B
; 0.0, 0.5, 1.0, 1.0, 0.0, -0.5, -0.5, 0.7, 0.0, 1.0, 0.5, -0.5, 0.1, 1.0, 0.6,
; ];
; let vertices = create_f32array(&vertices.into_bytes());
; let vertex_buffer = webgl::create_buffer(ctx);
; webgl::bind_buffer(ctx, webgl::ARRAY_BUFFER, vertex_buffer);
; webgl::buffer_data(ctx, webgl::ARRAY_BUFFER, vertices, webgl::STATIC_DRAW);
; webgl::bind_buffer(ctx, webgl::ARRAY_BUFFER, NULL);
;
; // setup for drawing
; webgl::use_program(ctx, program);
;
; // draw
; webgl::bind_buffer(ctx, webgl::ARRAY_BUFFER, vertex_buffer);
; webgl::enable_vertex_attrib_array(ctx, position_location);
; webgl::enable_vertex_attrib_array(ctx, color_location);
; webgl::vertex_attrib_pointer(
; ctx,
; position_location,
; 2.0,
; webgl::FLOAT,
; false,
; 5.0 * 4.0,
; 0.0,
; );
; webgl::vertex_attrib_pointer(
; ctx,
; color_location,
; 3.0,
; webgl::FLOAT,
; false,
; 5.0 * 4.0,
; 2.0 * 4.0,
; );
; webgl::bind_buffer(ctx, webgl::ARRAY_BUFFER, NULL);
;
; webgl::draw_arrays(ctx, webgl::TRIANGLES, 0.0, 3.0);
; }
Binary file added examples/webgl/webgl.wasm
Binary file not shown.
70 changes: 13 additions & 57 deletions src/compiler.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::ast::*;
use byteorder::{LittleEndian, WriteBytesExt};
use failure::Error;
use wasmly::WebAssembly::*;
use wasmly::*;
Expand All @@ -10,11 +9,6 @@ enum IdentifierType {
Function,
}

enum GlobalValueType {
Float(f64),
Int(i32),
}

struct Compiler {
wasm: wasmly::App,
ast: crate::ast::App,
Expand Down Expand Up @@ -95,10 +89,7 @@ impl Compiler {
for def in global_defs {
self.global_names.push(def.name.clone());
let v = self.get_global_value(&def.value);
match v {
GlobalValueType::Float(f) => self.global_values.push(f),
GlobalValueType::Int(i) => self.global_values.push(i as f64),
}
self.global_values.push(v);
}
}

Expand All @@ -108,31 +99,22 @@ impl Compiler {
bytes
}

fn int_to_bytes(&self, i: i32) -> Vec<u8> {
let mut bytes = vec![];
bytes.write_i32::<LittleEndian>(i).unwrap();
bytes
}

fn create_global_data(&mut self, v: Vec<GlobalValue>) -> i32 {
fn create_global_data(&mut self, v: Vec<GlobalValue>) -> f64 {
let mut bytes = vec![];
for i in 0..v.len() {
let v = self.get_global_value(&v[i]);
let b = match v {
GlobalValueType::Float(f) => self.float_to_bytes(f),
GlobalValueType::Int(i) => self.int_to_bytes(i),
};
let b = self.float_to_bytes(v);
bytes.extend_from_slice(&b);
}
self.create_data(bytes)
}

fn get_global_value(&mut self, v: &GlobalValue) -> GlobalValueType {
fn get_global_value(&mut self, v: &GlobalValue) -> f64 {
match v {
GlobalValue::Number(t) => GlobalValueType::Float(*t),
GlobalValue::Text(t) => GlobalValueType::Int(self.get_or_create_text_data(&t)),
GlobalValue::Data(t) => GlobalValueType::Int(self.create_global_data(t.clone())),
GlobalValue::Identifier(t) => GlobalValueType::Float(self.resolve_identifier(t).0),
GlobalValue::Number(t) => *t,
GlobalValue::Text(t) => self.get_or_create_text_data(&t),
GlobalValue::Data(t) => self.create_global_data(t.clone()),
GlobalValue::Identifier(t) => self.resolve_identifier(t).0,
}
}

Expand Down Expand Up @@ -192,14 +174,14 @@ impl Compiler {
.add_global(wasmly::Global::new(final_heap_pos as i32, true));
}

fn get_or_create_text_data(&mut self, str: &str) -> i32 {
fn get_or_create_text_data(&mut self, str: &str) -> f64 {
let mut bytes: Vec<u8> = str.as_bytes().into();
bytes.push(0);
self.create_data(bytes)
}

fn create_data(&mut self, bytes: Vec<u8>) -> i32 {
let pos = self.heap_position as i32;
fn create_data(&mut self, bytes: Vec<u8>) -> f64 {
let pos = self.heap_position;
let size = bytes.len();
self.wasm.add_data(Data::new(pos as i32, bytes));
let mut final_heap_pos = self.heap_position + (size as f64);
Expand Down Expand Up @@ -500,33 +482,7 @@ impl Compiler {
} else {
panic!("invalid number params for mem_heap_start")
}
} else if &x.function_name == "mem32" {
if x.params.len() == 1 {
self.process_expression(i, &x.params[0]);
self.function_implementations[i].with_instructions(vec![
I32_TRUNC_S_F64,
I32_LOAD,
(0 as i32).into(),
(0 as i32).into(),
F64_CONVERT_S_I32,
]);
} else if x.params.len() == 2 {
for k in 0..x.params.len() {
self.process_expression(i, &x.params[k]);
self.function_implementations[i]
.with_instructions(vec![I32_TRUNC_S_F64]);
}
self.function_implementations[i].with_instructions(vec![
I32_STORE,
(0 as i32).into(),
(0 as i32).into(),
]);
self.function_implementations[i]
.with_instructions(vec![F64_CONST, 0.0.into()]);
} else {
panic!("invalid number params for mem32")
}
} else if &x.function_name == "memf64" {
} else if &x.function_name == "mem_num" {
if x.params.len() == 1 {
self.process_expression(i, &x.params[0]);
self.function_implementations[i].with_instructions(vec![
Expand All @@ -547,7 +503,7 @@ impl Compiler {
self.function_implementations[i]
.with_instructions(vec![F64_CONST, 0.0.into()]);
} else {
panic!("invalid number params for mem32")
panic!("invalid number params for mem_num")
}
} else if &x.function_name == "=="
|| &x.function_name == "!="
Expand Down

0 comments on commit f818701

Please sign in to comment.