Skip to content

Commit

Permalink
new version
Browse files Browse the repository at this point in the history
  • Loading branch information
benmkw committed Feb 15, 2024
1 parent e77c36a commit 54b7d96
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 7 deletions.
22 changes: 21 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

<body>

<select name="testcases" id="testcases-select">
<option value="">--Open an example--</option>
</select>

<section>
<pre id="python_text">from dataclasses import dataclass

Expand Down Expand Up @@ -58,6 +62,7 @@

<script src="ace.js" type="text/javascript" charset="utf-8"></script>
<script src="ext-language_tools.js" type="text/javascript" charset="utf-8"></script>
<script src="testcases.js" type="text/javascript" charset="utf-8"></script>

<script type="module" async>
import init, { greet, p2r } from "./pkg/p2rjs.js";
Expand Down Expand Up @@ -135,7 +140,7 @@
document.getElementById("transpileErr").innerHTML = "";
document.getElementById("parseErr").innerHTML = "";
} else if (res.res_t == "TranspileError") {
document.getElementById("transpileErr").innerHTML = `TODO impl at file: ${res.file}:${res.line}`;
document.getElementById("transpileErr").innerHTML = `TODO impl at file: ${res.file}:${res.line} (https:://github.com/benmkw/p2r/blob/main/${res.file}#L${res.line})`;
// if transpilation had an error, the parser had a success
document.getElementById("parseErr").innerHTML = "";
} else if (res.res_t == "ParseError") {
Expand All @@ -146,6 +151,21 @@
}
}

{
const select = document.getElementById("testcases-select");
Object.keys(examples).forEach(function (example, _index) {
select.appendChild(new Option(example))
});

select.addEventListener("change", function (event) {
const sample = event.target.value;
// this is a bit of a hack
const sample_code = examples[sample]["python"].replace(/\\"/g, '"');

ace.edit("python_text").setValue(sample_code)
});
}

{
const editor = ace.edit("python_text");
editor.setKeyboardHandler("ace/keyboard/sublime");
Expand Down
16 changes: 10 additions & 6 deletions pkg/p2rjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ function passStringToWasm0(arg, malloc, realloc) {
const ret = encodeString(arg, view);

offset += ret.written;
ptr = realloc(ptr, len, offset, 1) >>> 0;
}

WASM_VECTOR_LEN = offset;
Expand All @@ -101,6 +102,9 @@ export function p2r(content) {
return Res.__wrap(ret);
}

const ResFinalization = (typeof FinalizationRegistry === 'undefined')
? { register: () => {}, unregister: () => {} }
: new FinalizationRegistry(ptr => wasm.__wbg_res_free(ptr >>> 0));
/**
*/
export class Res {
Expand All @@ -109,14 +113,14 @@ export class Res {
ptr = ptr >>> 0;
const obj = Object.create(Res.prototype);
obj.__wbg_ptr = ptr;

ResFinalization.register(obj, obj.__wbg_ptr, obj);
return obj;
}

__destroy_into_raw() {
const ptr = this.__wbg_ptr;
this.__wbg_ptr = 0;

ResFinalization.unregister(this);
return ptr;
}

Expand Down Expand Up @@ -155,7 +159,7 @@ export class Res {
let v1;
if (r0 !== 0) {
v1 = getStringFromWasm0(r0, r1).slice();
wasm.__wbindgen_free(r0, r1 * 1);
wasm.__wbindgen_free(r0, r1 * 1, 1);
}
return v1;
} finally {
Expand All @@ -174,7 +178,7 @@ export class Res {
let v1;
if (r0 !== 0) {
v1 = getStringFromWasm0(r0, r1).slice();
wasm.__wbindgen_free(r0, r1 * 1);
wasm.__wbindgen_free(r0, r1 * 1, 1);
}
return v1;
} finally {
Expand Down Expand Up @@ -207,7 +211,7 @@ export class Res {
let v1;
if (r0 !== 0) {
v1 = getStringFromWasm0(r0, r1).slice();
wasm.__wbindgen_free(r0, r1 * 1);
wasm.__wbindgen_free(r0, r1 * 1, 1);
}
return v1;
} finally {
Expand Down Expand Up @@ -250,7 +254,7 @@ async function __wbg_load(module, imports) {
function __wbg_get_imports() {
const imports = {};
imports.wbg = {};
imports.wbg.__wbg_alert_5410fc5b597b5c3a = function(arg0, arg1) {
imports.wbg.__wbg_alert_a348bfbade80cb34 = function(arg0, arg1) {
alert(getStringFromWasm0(arg0, arg1));
};
imports.wbg.__wbindgen_throw = function(arg0, arg1) {
Expand Down
Binary file modified pkg/p2rjs_bg.wasm
Binary file not shown.
102 changes: 102 additions & 0 deletions testcases.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// extracted from p2r/src/test.rs using tests_to_md.py
const examples = {
"simple": {
"python": "a = 0"
},
"comprehensions": {
"python": "arr = [x for x in range(5, 10)]\nset_res = {x for x in res if x in arr}\ndict_res = {k:2*v for k, v in zip(arr, arr)}"
},
"list_comprehension_with_if": {
"python": "[x for x in range(10) if x % 2 == 0]"
},
"basic_for_and_while": {
"python": "a = 4\nx = 0\nfor i in range(a):\n x += 1\n\nres = []\nwhile x > 0:\n x -= 1\n a = x**2\n res.append(a)"
},
"while_else": {
"python": "res = []\nwhile x > 0:\n x -= 1\nelse:\n print(\\\"done\\\")"
},
"basic_class": {
"python": "foo_i = Foo()\nclass Foo:\n a : int\n b : int\n\n def member_fn(self, other : int) -> int:\n return 2 * self.a ** other\n\nfoo_arg = Foo(1, 2*2)\nfoo_kw = Foo(b=4-1, a=1)"
},
"basic_enum": {
"python": "def EnumOrFun():\n pass\n\n# EnumOrFun is a function\nfoo_i = EnumOrFun()\n\nclass EnumOrFun(Enum):\n A = 1\n B = 2\n\n# EnumOrFun is a class\na_inst = EnumOrFun.A\n\nmatch a_inst:\n case EnumOrFun.A: print(\\\"got an A\\\")\n case EnumOrFun.B: print(\\\"got a B\\\")"
},
"tuple_fn": {
"python": "def t(x : int) -> Optional[tuple[int, int]]:\n if x < 3:\n return (3*x, 42)\n else:\n return None\n\na,b = t(1)"
},
"lists_sets": {
"python": "a = []\nb = [1,2,3]\nis_in = 42 in b\nc = {}\nd = {1,2,3}\ne = {\\\"a\\\" : 1, \\\"b\\\" : 2, \\\"c\\\" : 3}"
},
"lambda": {
"python": "times_two = lambda x : x * 2\ntwice = times_two(3)\nadd = lambda x,y : x + y\nadd(3,4)"
},
"else_if": {
"python": "DEBUG = True\nres = []\nif DEBUG and len(res) > 2:\n a = 42\n print(1)\nelif DEBUG:\n print(2)\nelif DEBUG2:\n print(3)\nelse:\n print(\\\"inside else...\\\")\n print(4)"
},
"control": {
"python": "if TRUE:\n break\n continue\n pass\nassert 4 == 2+2, \\\"Oh no\\\""
},
"math_polyfill": {
"python": "import math\narr = [1.1,2.1,3.1]\ntotal = math.pow(math.sqrt(math.cos(math.sin(sum(arr)))), 32.1)\nprint(total)"
},
"walrus": {
"python": "if x := map:\n print(f\\\"map ({map}) is {res[0] + 1}\\\")"
},
"slicing": {
"python": "res2 = [1,2,3,4]\nprint(res2[:3], res2[1:2:30])"
},
"json": {
"python": "foo_s = json.dumps(Foo())\nfoo_instance = json.loads((foo_s))"
},
"zip": {
"python": "a_arr = [1,2,3]\nb_arr = [4,5,6]\nc_arr = [7,8,9]\nfor a,b,c in zip(a_arr, b_arr, c_arr):\n print(f\\\"a: {a} b: {b}, c:{c}\\\")"
},
"print_fmt": {
"python": "a = 3\nb = f\\\"b is {2**2}\\\"\nprint(f\\\"a is {a}\\\")"
},
"type_alias": {
"python": "type PointFloat2 = tuple[float, float]\ntype MaybeFloat = Optional[float]\ntype ListOptionTuple = List[Optional[tuple[float, str]]]\ntype DictIntStr = Dict[int, str]"
},
"casts": {
"python": "i = int(3.2)\nf = float(3 + 1)\ns = str(1)"
},
"init": {
"python": "class A:\n i : int\n f : float\n\ndef __init__(self, i : int, f : float) -> A:\n self.i = i\n self.f = f"
},
"delete": {
"python": "a, b = 1, 2\ndel a, b"
},
"bytes": {
"python": "hello_world = b'\\x7f\\x45\\x4c\\x46\\x01\\x01\\x01\\x00'"
},
"raise": {
"python": "raise Exception('hello')"
},
"try_except": {
"python": "try:\n return 3 / 0\nexcept BaseException as e1:\n # TODO handle ; here better\n # in .rs code\n # print(e)\n val = 100\n return val"
},
"kwargs": {
"python": "def foo(x, a, b, c):\n pass\n\nfoo(0, a=1, b=2, c=3)"
},
"compare": {
"python": "a = 3 < 4\nb = 3 < 4 < 5"
},
"doc_comments": {
"python": "def add(a, b):\n \\\"\\\"\\\"Adds two numbers\n Arguments:\n a : Number\n b : Number\n \\\"\\\"\\\"\n return a + b"
},
"duffinian": {
"python": "# https://rosettacode.org/wiki/Duffinian_numbers#Python\n# with minor modifications (added typehints)\n\ndef factors(n : int) -> List[int]:\n factors = []\n for i in range(1, n + 1):\n if n % i == 0:\n factors.append(i)\n return factors\ndef gcd(a : int, b : int) -> int:\n while b != 0:\n a, b = b, a % b\n return a\nis_relively_prime = lambda a, b: gcd(a, b) == 1\nsigma_sum = lambda x: sum(factors(x))\nis_duffinian = lambda x: is_relively_prime(x, sigma_sum(x)) and len(factors(x)) > 2\ncount = 0\ni = 0\nwhile count < 50:\n if is_duffinian(i):\n print(i)\n count += 1\n i+=1\ncount2 = 0\nj = 0\nwhile count2 < 20:\n if is_duffinian(j) and is_duffinian(j+1) and is_duffinian(j+2):\n print(f\\\"({j},{j+1},{j+2})\\\")\n count2 += 1\n j+=3\n j+=1"
},
"fizzbufzz": {
"python": "# from https://rosettacode.org/wiki/FizzBuzz#Python\n\n# TODO\n# print (', '.join([(x%3<1)*'Fizz'+(x%5<1)*'Buzz' or str(x) for x in range(1,101)]))\n\n# TODO\n# print(*map(lambda n: 'Fizzbuzz '[(i):i+13] if (i := n**4%-15) > -14 else n, range(1,100)))\n\n[print('FizzBuzz') if i % 15 == 0 else print('Fizz') if i % 3 == 0 else print('Buzz') if i % 5 == 0 else print(i) for i in range(1,101)]"
},
"generator_expression": {
"python": "sum(x*x for x in range(10))"
},
"basic_rust_decorator": {
"python": "def math_arr_np(\n a: f64, x: NpReadonlyArrayDyn[f64], y: NpReadonlyArrayDyn[f64]\n) -> NpArrayDyn[f64]:\n return a * x + y"
},
"imports": {
"python": "from math import sin as foo, cos, pi as mypi\nimport math as math\nsin_of_pi = math.abs(cos(foo(mypi)))\nprint(sin_of_pi)"
}
};

0 comments on commit 54b7d96

Please sign in to comment.