Initial commit

This commit is contained in:
github-classroom[bot]
2021-04-04 18:11:58 +00:00
commit 9f1681bb23
342 changed files with 35807 additions and 0 deletions
+105
View File
@@ -0,0 +1,105 @@
import http.server
import socketserver
import json
import sys, subprocess, os, platform
classpath_separator = ';' if platform.system() == 'Windows' else ':'
def read_file(fn):
with open(fn,'r') as f:
return f.read()
def write_file(fn, x):
with open(fn,'w') as f:
return f.write(x)
def file_exists(fn):
return os.path.exists(fn)
class WebCompiler(http.server.SimpleHTTPRequestHandler):
def do_POST(self):
if self.path == "/compile":
content_len = int(self.headers.get('Content-Length', 0))
try:
request_json = self.rfile.read(content_len)
# Process request
try:
request = json.loads(request_json)
code = request["input"]
passes = request["passes"]
code_file = ".tmp.py"
result_file = ".tmp.py.out"
except Exception as e:
self.send_response(400)
self.send_header("Content-type", "text/plain")
self.end_headers()
self.wfile.write(str(e).encode('utf-8'))
return
# Verify that JARs exist
ref_jar = "../chocopy-ref.jar"
classpath = ref_jar
if file_exists(ref_jar):
fat_jar = "../target/assignment.jar"
classpath = classpath + classpath_separator + fat_jar
else:
fat_jar = "../target/chocopy-ref.jar"
classpath = fat_jar
if not file_exists(fat_jar):
raise Exception("Could not find file %s or %s" % (ref_jar, fat_jar))
# Compile
write_file(code_file, code)
stat = subprocess.run(["java", "-cp", classpath,
"chocopy.ChocoPy", "--pass", passes,
code_file, "--json", "--out", result_file], capture_output=True)
if stat.returncode not in [0, 2]:
raise Exception(stat.stderr.decode('utf-8'))
result_json = read_file(result_file)
# Clean up temps
os.remove(code_file)
os.remove(result_file)
# Send response
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
self.wfile.write(str(result_json).encode('utf-8'))
except Exception as e:
self.send_response(500)
self.send_header("Content-type", "text/plain")
self.end_headers()
self.wfile.write(str(e).encode('utf-8'))
return
else:
self.send_response(404)
self.end_headers()
return
if __name__ == '__main__':
PORT = int(sys.argv[1]) if len(sys.argv) > 1 else 8000
Handler = WebCompiler
with socketserver.TCPServer(("", PORT), Handler) as httpd:
print("serving at port", PORT)
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
View File
+461
View File
@@ -0,0 +1,461 @@
/*! normalize.css v5.0.0 | MIT License | github.com/necolas/normalize.css */
/**
* 1. Change the default font family in all browsers (opinionated).
* 2. Correct the line height in all browsers.
* 3. Prevent adjustments of font size after orientation changes in
* IE on Windows Phone and in iOS.
*/
/* Document
========================================================================== */
html {
font-family: sans-serif; /* 1 */
line-height: 1.15; /* 2 */
-ms-text-size-adjust: 100%; /* 3 */
-webkit-text-size-adjust: 100%; /* 3 */
}
/* Sections
========================================================================== */
/**
* Remove the margin in all browsers (opinionated).
*/
body {
margin: 0;
}
/**
* Add the correct display in IE 9-.
*/
article,
aside,
footer,
header,
nav,
section {
display: block;
}
/**
* Correct the font size and margin on `h1` elements within `section` and
* `article` contexts in Chrome, Firefox, and Safari.
*/
h1 {
font-size: 2em;
margin: 0.67em 0;
}
/* Grouping content
========================================================================== */
/**
* Add the correct display in IE 9-.
* 1. Add the correct display in IE.
*/
figcaption,
figure,
main { /* 1 */
display: block;
}
/**
* Add the correct margin in IE 8.
*/
figure {
margin: 1em 40px;
}
/**
* 1. Add the correct box sizing in Firefox.
* 2. Show the overflow in Edge and IE.
*/
hr {
box-sizing: content-box; /* 1 */
height: 0; /* 1 */
overflow: visible; /* 2 */
}
/**
* 1. Correct the inheritance and scaling of font size in all browsers.
* 2. Correct the odd `em` font sizing in all browsers.
*/
pre {
font-family: monospace, monospace; /* 1 */
font-size: 1em; /* 2 */
}
/* Text-level semantics
========================================================================== */
/**
* 1. Remove the gray background on active links in IE 10.
* 2. Remove gaps in links underline in iOS 8+ and Safari 8+.
*/
a {
background-color: transparent; /* 1 */
-webkit-text-decoration-skip: objects; /* 2 */
}
/**
* Remove the outline on focused links when they are also active or hovered
* in all browsers (opinionated).
*/
a:active,
a:hover {
outline-width: 0;
}
/**
* 1. Remove the bottom border in Firefox 39-.
* 2. Add the correct text decoration in Chrome, Edge, IE, Opera, and Safari.
*/
abbr[title] {
border-bottom: none; /* 1 */
text-decoration: underline; /* 2 */
text-decoration: underline dotted; /* 2 */
}
/**
* Prevent the duplicate application of `bolder` by the next rule in Safari 6.
*/
b,
strong {
font-weight: inherit;
}
/**
* Add the correct font weight in Chrome, Edge, and Safari.
*/
b,
strong {
font-weight: bolder;
}
/**
* 1. Correct the inheritance and scaling of font size in all browsers.
* 2. Correct the odd `em` font sizing in all browsers.
*/
code,
kbd,
samp {
font-family: monospace, monospace; /* 1 */
font-size: 1em; /* 2 */
}
/**
* Add the correct font style in Android 4.3-.
*/
dfn {
font-style: italic;
}
/**
* Add the correct background and color in IE 9-.
*/
mark {
background-color: #ff0;
color: #000;
}
/**
* Add the correct font size in all browsers.
*/
small {
font-size: 80%;
}
/**
* Prevent `sub` and `sup` elements from affecting the line height in
* all browsers.
*/
sub,
sup {
font-size: 75%;
line-height: 0;
position: relative;
vertical-align: baseline;
}
sub {
bottom: -0.25em;
}
sup {
top: -0.5em;
}
/* Embedded content
========================================================================== */
/**
* Add the correct display in IE 9-.
*/
audio,
video {
display: inline-block;
}
/**
* Add the correct display in iOS 4-7.
*/
audio:not([controls]) {
display: none;
height: 0;
}
/**
* Remove the border on images inside links in IE 10-.
*/
img {
border-style: none;
}
/**
* Hide the overflow in IE.
*/
svg:not(:root) {
overflow: hidden;
}
/* Forms
========================================================================== */
/**
* 1. Change the font styles in all browsers (opinionated).
* 2. Remove the margin in Firefox and Safari.
*/
button,
input,
optgroup,
select,
textarea {
font-family: sans-serif; /* 1 */
font-size: 100%; /* 1 */
line-height: 1.15; /* 1 */
margin: 0; /* 2 */
}
/**
* Show the overflow in IE.
* 1. Show the overflow in Edge.
*/
button,
input { /* 1 */
overflow: visible;
}
/**
* Remove the inheritance of text transform in Edge, Firefox, and IE.
* 1. Remove the inheritance of text transform in Firefox.
*/
button,
select { /* 1 */
text-transform: none;
}
/**
* 1. Prevent a WebKit bug where (2) destroys native `audio` and `video`
* controls in Android 4.
* 2. Correct the inability to style clickable types in iOS and Safari.
*/
button,
html [type="button"], /* 1 */
[type="reset"],
[type="submit"] {
-webkit-appearance: button; /* 2 */
}
/**
* Remove the inner border and padding in Firefox.
*/
button::-moz-focus-inner,
[type="button"]::-moz-focus-inner,
[type="reset"]::-moz-focus-inner,
[type="submit"]::-moz-focus-inner {
border-style: none;
padding: 0;
}
/**
* Restore the focus styles unset by the previous rule.
*/
button:-moz-focusring,
[type="button"]:-moz-focusring,
[type="reset"]:-moz-focusring,
[type="submit"]:-moz-focusring {
outline: 1px dotted ButtonText;
}
/**
* Change the border, margin, and padding in all browsers (opinionated).
*/
fieldset {
border: 1px solid #c0c0c0;
margin: 0 2px;
padding: 0.35em 0.625em 0.75em;
}
/**
* 1. Correct the text wrapping in Edge and IE.
* 2. Correct the color inheritance from `fieldset` elements in IE.
* 3. Remove the padding so developers are not caught out when they zero out
* `fieldset` elements in all browsers.
*/
legend {
box-sizing: border-box; /* 1 */
color: inherit; /* 2 */
display: table; /* 1 */
max-width: 100%; /* 1 */
padding: 0; /* 3 */
white-space: normal; /* 1 */
}
/**
* 1. Add the correct display in IE 9-.
* 2. Add the correct vertical alignment in Chrome, Firefox, and Opera.
*/
progress {
display: inline-block; /* 1 */
vertical-align: baseline; /* 2 */
}
/**
* Remove the default vertical scrollbar in IE.
*/
textarea {
overflow: auto;
}
/**
* 1. Add the correct box sizing in IE 10-.
* 2. Remove the padding in IE 10-.
*/
[type="checkbox"],
[type="radio"] {
box-sizing: border-box; /* 1 */
padding: 0; /* 2 */
}
/**
* Correct the cursor style of increment and decrement buttons in Chrome.
*/
[type="number"]::-webkit-inner-spin-button,
[type="number"]::-webkit-outer-spin-button {
height: auto;
}
/**
* 1. Correct the odd appearance in Chrome and Safari.
* 2. Correct the outline style in Safari.
*/
[type="search"] {
-webkit-appearance: textfield; /* 1 */
outline-offset: -2px; /* 2 */
}
/**
* Remove the inner padding and cancel buttons in Chrome and Safari on macOS.
*/
[type="search"]::-webkit-search-cancel-button,
[type="search"]::-webkit-search-decoration {
-webkit-appearance: none;
}
/**
* 1. Correct the inability to style clickable types in iOS and Safari.
* 2. Change font properties to `inherit` in Safari.
*/
::-webkit-file-upload-button {
-webkit-appearance: button; /* 1 */
font: inherit; /* 2 */
}
/* Interactive
========================================================================== */
/*
* Add the correct display in IE 9-.
* 1. Add the correct display in Edge, IE, and Firefox.
*/
details, /* 1 */
menu {
display: block;
}
/*
* Add the correct display in all browsers.
*/
summary {
display: list-item;
}
/* Scripting
========================================================================== */
/**
* Add the correct display in IE 9-.
*/
canvas {
display: inline-block;
}
/**
* Add the correct display in IE.
*/
template {
display: none;
}
/* Hidden
========================================================================== */
/**
* Add the correct display in IE 10-.
*/
[hidden] {
display: none;
}
+165
View File
@@ -0,0 +1,165 @@
/* Sakura.css v1.0.0
* ================
* Minimal css theme.
* Project: https://github.com/oxalorg/sakura
*/
/* Body */
html {
font-size: 62.5%;
font-family: Georgia, serif; }
body {
font-size: 1.9rem;
line-height: 1.618;
max-width: 48em;
margin: auto;
color: #4a4a4a;
background-color: #f9f9f9;
padding: 13px; }
@media (max-width: 684px) {
body {
font-size: 1.53rem; } }
@media (max-width: 382px) {
body {
font-size: 1.35rem; } }
h1, h2, h3, h4, h5, h6 {
line-height: 1.1;
font-family: Helvetica, Verdana, Geneva, sans-serif;
font-weight: 700;
overflow-wrap: break-word;
word-wrap: break-word;
-ms-word-break: break-all;
word-break: break-word;
-ms-hyphens: auto;
-moz-hyphens: auto;
-webkit-hyphens: auto;
hyphens: auto; }
h1 {
font-size: 2.35em; }
h2 {
font-size: 2.00em; }
h3 {
font-size: 1.75em; }
h4 {
font-size: 1.5em; }
h5 {
font-size: 1.25em; }
h6 {
font-size: 1em; }
small, sub, sup {
font-size: 75%; }
hr {
border-color: #2c8898; }
a {
text-decoration: none;
color: #2c8898; }
a:hover {
color: #982c61;
border-bottom: 2px solid #4a4a4a; }
ul {
padding-left: 1.4em; }
li {
margin-bottom: 0.4em; }
blockquote {
font-style: italic;
margin-left: 1.5em;
padding-left: 1em;
border-left: 3px solid #2c8898; }
img {
max-width: 100%; }
/* Pre and Code */
pre {
background-color: #f1f1f1;
display: block;
padding: 1em;
overflow-x: auto; }
code {
font-size: 0.9em;
padding: 0 0.5em;
background-color: #f1f1f1;
white-space: pre-wrap; }
pre > code {
padding: 0;
background-color: transparent;
white-space: pre; }
/* Tables */
table {
text-align: justify;
width: 100%;
border-collapse: collapse; }
td, th {
padding: 0.5em;
border-bottom: 1px solid #f1f1f1; }
/* Buttons, forms and input */
input, textarea {
border: 1px solid #4a4a4a; }
input:focus, textarea:focus {
border: 1px solid #2c8898; }
textarea {
width: 100%; }
.button, button, input[type="submit"], input[type="reset"], input[type="button"] {
display: inline-block;
padding: 5px 10px;
text-align: center;
text-decoration: none;
white-space: nowrap;
background-color: #2c8898;
color: #f9f9f9;
border-radius: 1px;
border: 1px solid #2c8898;
cursor: pointer;
box-sizing: border-box; }
.button[disabled], button[disabled], input[type="submit"][disabled], input[type="reset"][disabled], input[type="button"][disabled] {
cursor: default;
opacity: .5; }
.button:focus, .button:hover, button:focus, button:hover, input[type="submit"]:focus, input[type="submit"]:hover, input[type="reset"]:focus, input[type="reset"]:hover, input[type="button"]:focus, input[type="button"]:hover {
background-color: #982c61;
border-color: #982c61;
color: #f9f9f9;
outline: 0; }
textarea, select, input[type] {
color: #4a4a4a;
padding: 6px 10px;
/* The 6px vertically centers text on FF, ignored by Webkit */
margin-bottom: 10px;
background-color: #f1f1f1;
border: 1px solid #f1f1f1;
border-radius: 4px;
box-shadow: none;
box-sizing: border-box; }
textarea:focus, select:focus, input[type]:focus {
border: 1px solid #2c8898;
outline: 0; }
input[type="checkbox"]:focus {
outline: 1px dotted #2c8898; }
label, legend, fieldset {
display: block;
margin-bottom: .5rem;
font-weight: 600; }
File diff suppressed because one or more lines are too long
+177
View File
@@ -0,0 +1,177 @@
<!DOCTYPE html>
<html>
<head>
<title>ChocoPy Web IDE</title>
<link rel="stylesheet" href="css/normalize.css" />
<link rel="stylesheet" href="css/sakura.css" />
<script
src="js/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
<script src="js/ace.js" type="text/javascript" charset="utf-8"></script>
<style>
.example {
border: 1px solid black;
width: 50em;
height: 20em;
}
.compile-error-marker {
position: absolute;
background-color: rgba(255, 200, 200, 0.3);
}
.compile-button {
float: right;
}
</style>
</head>
<body>
<h1>Compile Me!</h1>
<select id="p1">
<option value="s">My Parser</option>
<option value="r" selected>Reference Parser</option>
</select>
<button id='parseButton' class='compile-button'>Parse Syntax</button>
<br />
<select id="p2">
<option value="s">My Semantic Analysis</option>
<option value="r" selected>Reference Semantic Analysis</option>
</select>
<button id='staticCheckButton' class='compile-button'>Run Static Checks</button>
<br />
<select id="p3">
<option value="s">My Code Generator</option>
<option value="r" selected>Reference Code Generator</option>
</select>
<button id='compileButton' class='compile-button'>Compile to RISC-V</button>
<pre class="example"></pre>
<script>
var compileService = "/compile";
var Range = require('ace/range').Range;
function makeEditor(example) {
var parseButton = $("#parseButton");
var staticCheckButton = $("#staticCheckButton");
var compileButton = $("#compileButton");
var editor = ace.edit(example, {
theme: "ace/theme/github",
mode: "ace/mode/python",
fontSize: "1em"
});
var errorMarkers = []
function clearMarkers() {
editor.session.clearAnnotations();
for (var marker of errorMarkers) {
editor.session.removeMarker(marker);
}
errorMarkers.length = 0;
}
editor.on('change', () => {
clearMarkers();
});
$(".compile-button").click((e) => {
var button = e.target.id;
console.assert(button == 'parseButton' ||
button == 'staticCheckButton' ||
button == 'compileButton');
var parse = true; // always run stage 1
var check = button != 'parseButton'; // run unless only stage 1 requested
var codegen = button == 'compileButton'; // run only when stage 3 requested
clearMarkers();
var code = editor.getValue();
var p1 = parse ? $("#p1").val() : "";
var p2 = check ? $("#p2").val() : "";
var p3 = codegen ? $("#p3").val() : "";
if (codegen) {
var resultWindow = window.open('venus.html?v=0.2.4' , '_blank');
if (resultWindow == null) {
console.error("Could not open new window...");
return; // Cannot show result
}
resultWindow.onload = () => resultWindow.codeMirror.setValue("# Compiling... Please be patient. This can take a few seconds.\n" +
"# (The first compilation takes the longest)");
}
$.ajax({
url: compileService,
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify({input: code, passes:p1+p2+p3}),
success: function(result) {
if (result.asm) {
var asm = result.asm;
console.log(resultWindow.codeMirror);
resultWindow.codeMirror.setValue("# Compiled ChocoPy Program to RISC-V assembly\n" +
"# Execute (run or step-through) using the 'Simulator' tab above \n" +
"# Output will appear on the bottom-left of the simulator\n" + asm);
} else if (result.errors && result.errors.errors && result.errors.errors.length > 0) {
if (codegen) { resultWindow.close(); }
var annotations = []
for (var error of result.errors.errors) {
var loc = error.location;
var msg = error.message;
var startLine = loc[0] - 1;
var startCol = loc[1] - 1;
var endLine = loc[2] - 1;
var endCol = loc[3] - 1;
var range = new Range(startLine, startCol, endLine, endCol+1);
errorMarkers.push(editor.session.addMarker(range, "compile-error-marker", "text", true));
annotations.push({
row: Math.min(Math.max(0, startLine), editor.session.getLength()-1),
text: msg,
type: "error"
});
}
editor.session.setAnnotations(annotations);
} else if (!codegen && result.kind && result.kind == "Program") {
if (check) {
alert("This program is semantically valid!");
} else {
alert("This program parses!");
}
} else {
alert("Unknown error: Should not reach here :-\\");
}
},
error: (xhr) => {
var w = window;
if (resultWindow) { w = resultWindow; }
w.alert("Request to compile service failed :-(\n\n" +
(xhr.responseText || "Unknown error"));
if (resultWindow) { resultWindow.close(); }
}
});
});
}
$(".example").each((i, e) => makeEditor(e))
</script>
</body>
</html>
+17
View File
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+2
View File
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+8
View File
@@ -0,0 +1,8 @@
define("ace/theme/github",["require","exports","module","ace/lib/dom"],function(e,t,n){t.isDark=!1,t.cssClass="ace-github",t.cssText='.ace-github .ace_gutter {background: #e8e8e8;color: #AAA;}.ace-github {background: #fff;color: #000;}.ace-github .ace_keyword {font-weight: bold;}.ace-github .ace_string {color: #D14;}.ace-github .ace_variable.ace_class {color: teal;}.ace-github .ace_constant.ace_numeric {color: #099;}.ace-github .ace_constant.ace_buildin {color: #0086B3;}.ace-github .ace_support.ace_function {color: #0086B3;}.ace-github .ace_comment {color: #998;font-style: italic;}.ace-github .ace_variable.ace_language {color: #0086B3;}.ace-github .ace_paren {font-weight: bold;}.ace-github .ace_boolean {font-weight: bold;}.ace-github .ace_string.ace_regexp {color: #009926;font-weight: normal;}.ace-github .ace_variable.ace_instance {color: teal;}.ace-github .ace_constant.ace_language {font-weight: bold;}.ace-github .ace_cursor {color: black;}.ace-github.ace_focus .ace_marker-layer .ace_active-line {background: rgb(255, 255, 204);}.ace-github .ace_marker-layer .ace_active-line {background: rgb(245, 245, 245);}.ace-github .ace_marker-layer .ace_selection {background: rgb(181, 213, 255);}.ace-github.ace_multiselect .ace_selection.ace_start {box-shadow: 0 0 3px 0px white;}.ace-github.ace_nobold .ace_line > span {font-weight: normal !important;}.ace-github .ace_marker-layer .ace_step {background: rgb(252, 255, 0);}.ace-github .ace_marker-layer .ace_stack {background: rgb(164, 229, 101);}.ace-github .ace_marker-layer .ace_bracket {margin: -1px 0 0 -1px;border: 1px solid rgb(192, 192, 192);}.ace-github .ace_gutter-active-line {background-color : rgba(0, 0, 0, 0.07);}.ace-github .ace_marker-layer .ace_selected-word {background: rgb(250, 250, 255);border: 1px solid rgb(200, 200, 250);}.ace-github .ace_invisible {color: #BFBFBF}.ace-github .ace_print-margin {width: 1px;background: #e8e8e8;}.ace-github .ace_indent-guide {background: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAACCAYAAACZgbYnAAAAE0lEQVQImWP4////f4bLly//BwAmVgd1/w11/gAAAABJRU5ErkJggg==") right repeat-y;}';var r=e("../lib/dom");r.importCssString(t.cssText,t.cssClass)}); (function() {
window.require(["ace/theme/github"], function(m) {
if (typeof module == "object" && typeof exports == "object" && module) {
module.exports = m;
}
});
})();
+12
View File
File diff suppressed because one or more lines are too long
+3
View File
File diff suppressed because one or more lines are too long