Skip to content

Commit

Permalink
Merge pull request #91 from maryvilledev/cs_90
Browse files Browse the repository at this point in the history
Add C, Clojure, Java (not java8), and Lua #90
  • Loading branch information
smokinGun authored May 19, 2017
2 parents a27137b + 4543cec commit b719554
Show file tree
Hide file tree
Showing 44 changed files with 1,453 additions and 99 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
[submodule "grammars-v4"]
path = grammars-v4
url = https://github.com/maryvilledev/grammars-v4.git
[submodule "antlr4"]
path = antlr4
url = https://github.com/maryvilledev/antlr4.git
7 changes: 6 additions & 1 deletion __tests__/transformers/simplify_node.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,20 @@ describe(`simplify_node.js`, () => {
• end
• tags
• children`, () => {

// Different forms for starts and stops are intentionally
// used here, as both can be found in real ANTLR nodes.
const input_node = (
makeAntlrNode(0, { start: 0 }, { stop: 5 }, [
makeAntlrNode(1, { start: 0, stop: 4 }, undefined, undefined),
makeAntlrNode(1, { start: 0, stop: 4 }, undefined, []),
]));

const { rule_name_map } = lang_runtime_config;
const expected = (
makeNode(rule_name_map[0], 0, 5 + 1, [], [
makeNode(rule_name_map[1], 0, 4 + 1, [], []),
]));

expect(simplify_node(lang_runtime_config, input_node)).toEqual(expected);
});

Expand All @@ -51,13 +53,16 @@ describe(`simplify_node.js`, () => {
• tags
• children
• text`, () => {

const input_terminal = (
makeAntlrTerminal(0, 0, 5, 'foobar')
);

const { symbol_name_map } = lang_runtime_config;
const expected = (
makeTerminal(symbol_name_map[0 + 2], 0, 6, 'foobar', [])
);

expect(simplify_node(lang_runtime_config, input_terminal)).toEqual(expected);
});
});
18 changes: 18 additions & 0 deletions __tests__/tree/code/snippet.1v88olr0.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(defn run [nvecs nitems nthreads niters]
(let [vec-refs (->> (range (* nvecs nitems)) (partition nitems) (map (comp ref vec)) vec)
swap #(let [v1 (rand-int nvecs)
v2 (rand-int nvecs)
i1 (rand-int nitems)
i2 (rand-int nitems)]
(dosync
(let [tmp (nth @(vec-refs v1) i1)]
(alter (vec-refs v1) assoc i1 (nth @(vec-refs v2) i2))
(alter (vec-refs v2) assoc i2 tmp))))
report #(let [derefed (map deref vec-refs)]
(prn derefed)
(println "Distinct:" (->> derefed (apply concat) distinct count)))]
(report)
(dorun (apply pcalls (repeat nthreads #(dotimes [_ niters] (swap)))))
(report)))

(run 100 10 10 100000)
69 changes: 69 additions & 0 deletions __tests__/tree/code/snippet.ecemhuve.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Just a concatenation of some of the snippets from https://en.wikipedia.org/wiki/C_syntax

enum colors { RED, GREEN, BLUE = 5, YELLOW } paint_color;

struct thing {
int num;
}; /* thing struct type is now completed */

typedef struct Bert Bert;
typedef struct Wilma Wilma;

struct Bert {
Wilma *wilma;
};

struct Wilma {
Bert *bert;
};

union u {
int x;
float y;
char *z;
} n;

struct f {
unsigned int flag : 1; /* a bit flag: can either be on (1) or off (0) */
signed int num : 4; /* a signed 4-bit field; range -7...7 or -8...7 */
signed int : 3; /* 3 bits of padding to round out to 8 bits */
} g;

void func() {
int *p;
int a, b;
int (*ptr_to_array)[100] = &array;

a = 10;
p = &a;
b = *p;
a = malloc(n * sizeof(int));
a[3] = 10;

for (int i = 0; i < limit; ++i) {
printf("helloworld.c: %d: Hello world\n", i);
}
}

int (*operation)(int x, int y);

int add(int x, int y)
{
return x + y;
}

int subtract(int x, int y)
{
return x - y;
}

int main(int argc, char* args[])
{
int foo = 1, bar = 1;

operation = add;
printf("%d + %d = %d\n", foo, bar, operation(foo, bar));
operation = subtract;
printf("%d - %d = %d\n", foo, bar, operation(foo, bar));
return 0;
}
1 change: 0 additions & 1 deletion __tests__/tree/code/snippet.kt29xnfw.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@
print('sum =', total)
except ValueError:
print('Please supply integer arguments')

44 changes: 44 additions & 0 deletions __tests__/tree/code/snippet.lk0rlo4z.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Just a concatenation of some of the snippets from https://en.wikipedia.org/wiki/Java_(programming_language)

// This is an example of a single line comment using two slashes

/* This is an example of a multiple line comment using the slash and asterisk.
This type of comment can be used to hold a lot of information or deactivate
code, but it is very important to remember to close the comment. */

package fibsandlies;
import java.util.HashMap;

/**
* This is an example of a Javadoc comment; Javadoc can compile documentation
* from this text. Javadoc comments must immediately precede the class, method, or field being documented.
*/
public class FibCalculator extends Fibonacci implements Calculator {
private static Map<Integer, Integer> memoized = new HashMap<Integer, Integer>();

/*
* The main method written as follows is used by the JVM as a starting point for the program.
*/
public static void main(String[] args) {
memoized.put(1, 1);
memoized.put(2, 1);
System.out.println(fibonacci(12)); //Get the 12th Fibonacci number and print to console
}

/**
* An example of a method written in Java, wrapped in a class.
* Given a non-negative number FIBINDEX, returns
* the Nth Fibonacci number, where N equals FIBINDEX.
* @param fibIndex The index of the Fibonacci number
* @return The Fibonacci number
*/
public static int fibonacci(int fibIndex) {
if (memoized.containsKey(fibIndex)) {
return memoized.get(fibIndex);
} else {
int answer = fibonacci(fibIndex - 1) + fibonacci(fibIndex - 2);
memoized.put(fibIndex, answer);
return answer;
}
}
}
69 changes: 69 additions & 0 deletions __tests__/tree/code/snippet.seefwj9h.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
-- Just a concatenation of some of the snippets from https://en.wikipedia.org/wiki/Lua_(programming_language)

function factorial(n)
local x = 1
for i = 2, n do
x = x * i
end
return x
end

function addto(x)
-- Return a new function that adds x to the argument
return function(y)
--[=[ When we refer to the variable x, which is outside of the current
scope and whose lifetime would be shorter than that of this anonymous
function, Lua creates a closure.]=]
return x + y
end
end
fourplus = addto(4)
print(fourplus(3)) -- Prints 7

--This can also be achieved by calling the function in the following way:
print(addto(4)(3))
--[[ This is because we are calling the returned function from `addto(4)' with the argument `3' directly.
This also helps to reduce data cost and up performance if being called iteratively.
]]

a_table = {x = 10} -- Creates a new table, with one entry mapping "x" to the number 10.
print(a_table["x"]) -- Prints the value associated with the string key, in this case 10.
b_table = a_table
b_table["x"] = 20 -- The value in the table has been changed to 20.
print(b_table["x"]) -- Prints 20.
print(a_table["x"]) -- Also prints 20, because a_table and b_table both refer to the same table.

Point = {}

Point.new = function(x, y)
return {x = x, y = y} -- return {["x"] = x, ["y"] = y}
end

Point.set_x = function(point, x)
point.x = x -- point["x"] = x;
end

fibs = { 1, 1 } -- Initial values for fibs[1] and fibs[2].
setmetatable(fibs, {
__index = function(values, n) --[[ __index is a function predefined by Lua,
it is called if key "n" does not exist. ]]
values[n] = values[n - 1] + values[n - 2] -- Calculate and memoize fibs[n].
return values[n]
end
})

local Vector = {}
Vector.__index = Vector

function Vector:new(x, y, z) -- The constructor
return setmetatable({x = x, y = y, z = z}, Vector)
end

function Vector:magnitude() -- Another method
-- Reference the implicit object using self
return math.sqrt(self.x^2 + self.y^2 + self.z^2)
end

local vec = Vector:new(0, 1, 0) -- Create a vector
print(vec:magnitude()) -- Call a method (output: 1)
print(vec.x) -- Access a member variable (output: 0)
32 changes: 22 additions & 10 deletions __tests__/tree/compare.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const path = require('path');
const fs = require('fs-promise');
const antlr = require('antlr4');
const expect_error = require('../../src/utils/expect_error.js');
const run_antlr = require('../../src/utils/run_antlr.js');
const run_java = require('../../src/utils/run_java.js');
Expand Down Expand Up @@ -79,13 +78,11 @@ const prepareJs = async function(lang_compile_config, lang_runtime_config) {
}

const genTreeViaJs = async function(lang_runtime_config, js_parser, code) {
const tree = js_parser(code, function(err) {
return js_parser(code, function(err) {
throw err;
}, {
'return_antlr_tree': true,
'return_antlr_toStringTree': true,
});

return tree.toStringTree(tree.parser.ruleNames);
};


Expand Down Expand Up @@ -123,7 +120,9 @@ const compare = async function(language_name, code_file) {
console.log('compare(' + code_file + ') genJava: ' + (t4 - t3) + 'ms');
console.log('compare(' + code_file + ') genJs: ' + (t5 - t4) + 'ms');

return treeViaJava.trim() === treeViaJs.trim();
return [treeViaJava, treeViaJs].map(function(val) {
return val.trim().replace(/(\\n|\s)+/g, ' ');
});
};


Expand All @@ -145,19 +144,32 @@ describe('grammars-v4/', () => {
it(description, () => {
expect.assertions(1);
return compare(lang_key, __dirname + '/code/' + code_filename).then(data => {
expect(data).toBeTruthy();
expect(data[0]).toEqual(data[1]);
}).catch(err => {
console.error(err);
});
});
};

describe('grammars-v4/python3/', () => {
test_snippet('python3', 'handles basic hello worlds', 'snippet.voc84cjo.py');
test_snippet('python3', 'handles a script adding the input arguments', 'snippet.kt29xnfw.py');
describe('grammars-v4/c/', () => {
test_snippet('c', 'handles lots of stuff', 'snippet.ecemhuve.c');
});

describe('grammars-v4/clojure/', () => {
test_snippet('clojure', 'handles lots of stuff', 'snippet.1v88olr0.clj');
})

describe('grammars-v4/java8/', () => {
test_snippet('java8', 'handles basic hello worlds', 'snippet.k17eu4f2.java');
test_snippet('java8', 'handles lots of stuff', 'snippet.lk0rlo4z.java');
});

describe('grammars-v4/lua/', () => {
test_snippet('lua', 'handles lots of stuff', 'snippet.seefwj9h.lua');
});

describe('grammars-v4/python3/', () => {
test_snippet('python3', 'handles basic hello worlds', 'snippet.voc84cjo.py');
test_snippet('python3', 'handles a script adding the input arguments', 'snippet.kt29xnfw.py');
});
});
1 change: 0 additions & 1 deletion antlr4
Submodule antlr4 deleted from 2ebae9
2 changes: 1 addition & 1 deletion grammars-v4
7 changes: 0 additions & 7 deletions install
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,3 @@ git submodule update --init --recursive
mkdir -p bin/
curl http://www.antlr.org/download/antlr-4.6-complete.jar -o bin/antlr-4.6-complete.jar

cd antlr4/runtime/JavaScript/src/antlr4/
npm install
cd -
npm link antlr4/runtime/JavaScript/src/antlr4/

mkdir -p _cache/
mkdir -p _cache/java_func_data/
9 changes: 9 additions & 0 deletions language_configs/c.compile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Configuration that can only be read by the compiler.

module.exports = {
'grammar_files': {
'Java': 'grammars-v4/c/C.g4',
'TypeScript': 'grammars-v4/c/C.g4',
},
'tree_matcher_specs': require('./c.tree_matcher_specs.js'),
};
Loading

0 comments on commit b719554

Please sign in to comment.