Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/vereis/jarlang
Browse files Browse the repository at this point in the history
  • Loading branch information
vereis committed Mar 28, 2018
2 parents e905cf9 + 15b1f37 commit 77ec18f
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 74 deletions.
83 changes: 37 additions & 46 deletions doc/Technical_Report/Jarlang Technical Report.tex

Large diffs are not rendered by default.

13 changes: 4 additions & 9 deletions src/erl/asttrans.erl
Original file line number Diff line number Diff line change
Expand Up @@ -785,13 +785,8 @@ recurse_var_assignments(ConsCount, {c_var, _, Name}, V, isTail) ->
<<"=">>,
estree:identifier(atom_to_binary(Name, utf8)),
estree:call_expression(
estree:member_expression(V, estree:identifier(<<"slice">>), false),
[ estree:literal(ConsCount),
estree:call_expression(
estree:member_expression(V, estree:identifier(<<"size">>), false),
[]
)
]
estree:member_expression(V, estree:identifier(<<"nthSeg">>), false),
[estree:literal(ConsCount)]
)
)
];
Expand All @@ -802,13 +797,13 @@ recurse_var_assignments(_, {c_tuple, _, Elements}, V, _) ->
lists:append(
L,
recurse_var_assignments(0, Elem,
estree:expression_statement(
%estree:expression_statement(
estree:call_expression(
estree:member_expression(V,
estree:identifier(<<"nth">>), false),
[estree:literal(I)]
)
),
,%),
false
)
)}
Expand Down
4 changes: 2 additions & 2 deletions src/js/classes/datatype_int.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const Int = (() => {
}

match(other) {
if (other===null||(!isNaN(other) && this.equals(other))) {
return other;
if (other==null||(!isNaN(other) && this.equals(other))) {
return this;
}
else {
return undefined;
Expand Down
44 changes: 28 additions & 16 deletions src/js/classes/datatype_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const List = (() => {
[nthNode](n) {
if (n < 0 || n >= this.size()) {
throw "index out of bounds error";
}
}

let i = 0;
let walker = this;
Expand All @@ -58,7 +58,7 @@ const List = (() => {
next: () => {
// If the next node of the current iterator isn't another list OR is an empty list, then we know
// we have reached the end of the linked list
let isLastNode = this.iterator.next === undefined || List.isEmptyList(this.iterator.next);
let isLastNode = !this.iterator || this.iterator.next === undefined || List.isEmptyList(this.iterator.next);
let v = List.isList(this.iterator) ? this.iterator.value : this.iterator;

if (this.iterator === "done" || List.isEmptyList(this)) {
Expand All @@ -83,6 +83,10 @@ const List = (() => {
return List.isList(nth) ? nth.value : nth;
}

nthSeg(n){
return this[nthNode](n);
}

size() {
return [...this].length;
}
Expand Down Expand Up @@ -128,21 +132,29 @@ const List = (() => {
}

match(other) {
if(other===null)return other;
if (List.isList(other) && this.size() === other.size()) {
for (let i = 0; i < this.size(); i++) {
if (ErlangDatatype.isErlangDatatype(this.nth(i))) {
if (this.nth(i).match(other.nth(i)) === undefined) {
return undefined;
}
}
else {
if (this.nth(i) !== other.nth(i) && other.nth(i) !== null) {
return undefined;
}
}
if(other===null)return this;
if (List.isList(other)) {

if(this.nth(0).match(other.nth(0))){//If the first values match
if(other.next==null)return this;//Improper list (tail match)
let a= this.next().match(other.next());
if(a!=undefined) return this;
}
return other;
return undefined;
// for (let i = 0; i < this.size(); i++) {
// if (ErlangDatatype.isErlangDatatype(this.nth(i))) {
// if(other===null)return this;
// if (this.nth(i).match(other.nth(i)) === undefined) {
// return undefined;
// }
// }
// else {
// if (this.nth(i) !== other.nth(i) && other.nth(i) !== null) {
// return undefined;
// }
// }
// }
// return other;
}
else {
return undefined;
Expand Down
2 changes: 1 addition & 1 deletion src/js/classes/datatype_tuple.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const Tuple = (() => {

match(other) {
if(other===null)return other;
if (List.isTuple(other) && this.size() === other.size()) {
if (Tuple.isTuple(other) && this.size() === other.size()) {
for (let i = 0; i < this.size(); i++) {
if (ErlangDatatype.isErlangDatatype(this.nth(i))) {
if (this.nth(i).match(other.nth(i)) === undefined) {
Expand Down
3 changes: 3 additions & 0 deletions src/js/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ const jrts = (function (secondsPerTick) {
case "string":
erlVar = new List(...(variable.split("").map(char => new Int(char.charCodeAt(0)))));
break;
case "boolean":
erlVar = new Atom(variable.toString());
break;
case "object":
if (Array.isArray(variable)) {
erlVar = new List(...(variable.map(jsToErlang)));
Expand Down
66 changes: 66 additions & 0 deletions src/misc/demo.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
-module(demo).
-compile(export_all).

stdout_example(String) ->
io:format("~s", String).

arith_example() ->
A = 13 * 5,
B = 99.5 / 12,
C = 3 div 2,
D = 12 rem 4,
E = 15 rem 4,
io:format("~p, ~p, ~p, ~n ~p, ~p", [A, B, C, D, E]).

list_example(A,B,C)->
io:format("~p",[[[A,[B]],C]]).

tuple_example(A,B,C)->
io:format("~p",[{{A,{B}},C}]).

pattern_matching([_|Tail],{apple,_,_})->
io:format("~p~n~p",[Tail,apple]).

%####################################################
fact(N) ->
io:format("~p",[fact(N, 1)]).

fact(0, Res) ->
Res;
fact(N, Res) ->
fact(N - 1, Res * N).

%####################################################
fib(0) -> 0;
fib(1) -> 1;
fib(N) -> fib(N-1) + fib(N-2).

fibbonaci(N)->
io:format("~p",[fib(N)]).

%####################################################
ping(0, Pong_PID) ->
Pong_PID ! finished,
io:format("ping finished~n", []);

ping(N, Pong_PID) ->
Pong_PID ! {ping, self()},
receive
pong ->
io:format("Ping received pong~n", [])
end,
ping(N - 1, Pong_PID).

pong() ->
receive
finished ->
io:format("Pong finished~n", []);
{ping, Ping_PID} ->
io:format("Pong received ping~n", []),
Ping_PID ! pong,
pong()
end.

start() ->
Pong_PID = spawn(?MODULE, pong, []),
spawn(?MODULE, ping, [3, Pong_PID]).

0 comments on commit 77ec18f

Please sign in to comment.