diff --git a/old/content/Basics/5.md b/old/content/Basics/5.md index c704ceb..4d13b19 100644 --- a/old/content/Basics/5.md +++ b/old/content/Basics/5.md @@ -1,7 +1,8 @@ --- -title: "For" +title: "For Loop" weight: 5 --- {{}} +- standard `for` loop syntax +- same syntax as: C and JavaScript {{}} - diff --git a/old/content/Basics/7.md b/old/content/Basics/7.md index e4ee7af..1d86e40 100644 --- a/old/content/Basics/7.md +++ b/old/content/Basics/7.md @@ -3,5 +3,6 @@ title: "If" weight: 7 --- {{}} +- standard `if` syntax +- syntax same as: C and JavaScript {{}} - diff --git a/old/content/Basics/8.md b/old/content/Basics/8.md index 071807b..07ffa63 100644 --- a/old/content/Basics/8.md +++ b/old/content/Basics/8.md @@ -1,8 +1,9 @@ --- -title: "Switch" +title: "Switch Case" weight: 8 --- {{}} -- `case` statements automatically break. +- C3 switch/case syntax +- C3 `case` statements automatically break. +- note that `case` statements in C and JavaScript do not automatically break {{}} - diff --git a/old/content/Basics/9.md b/old/content/Basics/9.md index 76ef116..6fae46e 100644 --- a/old/content/Basics/9.md +++ b/old/content/Basics/9.md @@ -1,9 +1,8 @@ --- -title: "Nextcase" +title: "Switch Nextcase" weight: 9 --- {{}} - Use `nextcase` to fallthrough to the next statement. - Empty case statements have implicit fallthrough. {{}} - diff --git a/old/content/Basics/wasm1.md b/old/content/Basics/wasm1.md new file mode 100644 index 0000000..61672d0 --- /dev/null +++ b/old/content/Basics/wasm1.md @@ -0,0 +1,15 @@ +--- +title: "WASM @extern" +--- +- @wasm +- @extern("NAME") +- A function that is to be called from JavaScript should be marked with @wasm and @extern +- From JavaScript these functions are exposed as: wasm.instance.exports.NAME +``` +fn void onclick( int x, int y ) @extern("onclick") @wasm { + js_eval(` + window.alert("hello click") + `); +} +``` +- Above, the function is exported to JavaScript and callable as: `wasm.instance.exports.onclick(x,y)` diff --git a/old/content/Basics/wasm2.md b/old/content/Basics/wasm2.md new file mode 100644 index 0000000..d33c862 --- /dev/null +++ b/old/content/Basics/wasm2.md @@ -0,0 +1,22 @@ +--- +title: "WASM external functions" +--- +- External functions are provided by the runtime environment or the linker. +- When C3 compiles for the WASM target the runtime environment is a JavaScript object. +- In the example below `js_eval` is declared as an external function that is provided by the JavaScript object. +``` +extern fn int js_eval(char*ptr); + +fn void onclick( int x, int y ) @extern("onclick") @wasm { + js_eval(` + window.alert("hello click") + `); +} +``` +- In JavaScript `js_eval` is defined as: +``` + js_eval(a){ + return eval(cstr_by_ptr(this.wasm.instance.exports.memory.buffer,a)) + } + +``` diff --git a/old/content/Basics/wasm3.md b/old/content/Basics/wasm3.md new file mode 100644 index 0000000..40fee9d --- /dev/null +++ b/old/content/Basics/wasm3.md @@ -0,0 +1,57 @@ +--- +title: "WASM JavaScript Bridge" +--- +- WASM is loaded by JavaScript using: `WebAssembly.instantiate`. +- The first argument to `WebAssembly.instantiate` is the wasm file (ArrayBuffer of bytes) +- The second argument to `WebAssembly.instantiate` is an Object with `env`. +``` +extern fn int js_eval(char*ptr); + +fn void onclick( int x, int y ) @extern("onclick") @wasm { + js_eval(` + window.alert("hello click") + `); +} +``` +- The above C3 code calls `js_eval` passing a string to eval in JavaScript. +- Below is an example JavaScript bridge (`env`). +``` +class myapi{ + reset(wasm){ + this.wasm=wasm; + } + get_env(){ + return make_environment(this) + } + js_eval(a){ + return eval(cstr_by_ptr(this.wasm.instance.exports.memory.buffer,a)) + } + onclick(e){ + console.log("onclick:", e); + this.wasm.instance.exports.onclick(e.x,e.y) + } +} +``` +- Above requires these JavaScript helper functions +``` +function make_environment(e){ + return new Proxy(e,{ + get(t,p,r) { + if(e[p]!==undefined){return e[p].bind(e)} + return(...args)=>{throw p} + } + }); +} + +function cstrlen(m,p){ + var l=0; + while(m[p]!=0){l++;p++} + return l; +} + +function cstr_by_ptr(m,p){ + const l=cstrlen(new Uint8Array(m),p); + const b=new Uint8Array(m,p,l); + return new TextDecoder().decode(b) +} +``` diff --git a/old/content/More/syntax-error1.md b/old/content/More/syntax-error1.md new file mode 100644 index 0000000..5bcc1d5 --- /dev/null +++ b/old/content/More/syntax-error1.md @@ -0,0 +1,15 @@ +--- +title: "Error: Expected ';'" +--- +- A statement in C3 ends with a semicolon `;` +- Below: a simple function call statement, the line ends with `;` +``` +some_function_call(); +``` +- Note that Structs and Unions are not terminated with `;`. +- Below is invalid and will throw: `Error: ';' wasn't expected here, try removing it.` +``` +struct MyStruct{ + int a; +}; +``` diff --git a/old/content/More/syntax-error2.md b/old/content/More/syntax-error2.md new file mode 100644 index 0000000..0c35aaf --- /dev/null +++ b/old/content/More/syntax-error2.md @@ -0,0 +1,14 @@ +--- +title: "Error: Expected the ending ')' here." +--- +- A function call in C3 begins with the function name, +- followed by the function arguments in parenthesis. +- FUNCTION_NAME( OPTIONAL_ARGUMENTS,... ) +``` +some_function_call( a, b, c); +``` +- Below, is missing the closing parenthesis, and throws: `Error: Expected an ending ')'. Did you forget a ')' before this ';'?` +``` +some_function_call(a,b,c; +``` + diff --git a/old/content/More/syntax-error3.md b/old/content/More/syntax-error3.md new file mode 100644 index 0000000..638fef8 --- /dev/null +++ b/old/content/More/syntax-error3.md @@ -0,0 +1,13 @@ +--- +title: "Error: A type name was expected" +--- +- C3 functions are defined as: `fn RETURN_TYPE NAME ( ARGS...)`, +- Just like in C the return type of the function comes before the function name. +``` +fn void myfunc() {} +``` +- Below, is invalid, the return type is missing: +``` +fn myfunc() {} +``` +- Above will throw this error: `Error: A type name was expected, but this looks a variable or function name (as it doesn't start with an uppercase letter).`