Skip to content

Commit 1e33f78

Browse files
authored
Merge pull request #7 from HTML-as-programming-language/Arduino
added support for doctype and added includeFile to utils
2 parents 2c093ae + f32e783 commit 1e33f78

File tree

7 files changed

+61
-9
lines changed

7 files changed

+61
-9
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from elements.element import Element
2+
from utils import includeFile
3+
4+
5+
class Doctype(Element):
6+
""""
7+
Een variabel om te bepalen wat voor platform de code voor bedoeld is (e.g: arduino) html is pc
8+
HTML: <!DOCTYPE html>
9+
"""
10+
11+
def to_c(self):
12+
if self.attributes.get("text", "") == "<!DOCTYPE arduino>":
13+
return includeFile("libraries/arduinoLib.c") + "\n"
14+
else:
15+
return self.attributes.get("text", "") + "\n" #TODO niet dit

HTML_to_C_compiler/html_parser.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ def handle_closingtag(self, tagname, line):
3434
def finish_parsing(self):
3535
pass
3636

37+
def handle_doctype(self, doctype, line):
38+
pass
39+
3740
def feed(self, filepath, handler):
3841
"""
3942
call this to start reading a HTML file
@@ -60,6 +63,11 @@ def feed(self, filepath, handler):
6063
handler.handle_comment(tag[4:][:-3], start_line)
6164
continue
6265

66+
if tagname == "!DOCTYPE":
67+
#The great rule of life is to have no schemes but one unalterable purpose
68+
handler.handle_doctype(tag, start_line)
69+
continue
70+
6371
data = tag.split(">")[1]
6472

6573
is_self_closing_tag = tagname_and_attrs.endswith("/") # <p/> is a self closing tag

HTML_to_C_compiler/lexer.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from elements.var import Var
1010
from elements.loop import Loop
1111
from elements.link import Link, Script
12+
from elements.doctype import Doctype
1213
from html_parser import HTMLParser
1314
from utils import camel_case_to_hyphenated
1415

@@ -47,7 +48,8 @@ def __init__(self, dir, filename):
4748
Return,
4849
Comment, # <!-- this is a comment --> OR <comment text="this is a comment"/>
4950
Link, # <link type="text/html" href="./include-this-file.html"/>
50-
Script # <script type="text/html" src="./include-this-file.html"/>
51+
Script, # <script type="text/html" src="./include-this-file.html"/>
52+
Doctype
5153
]
5254

5355
def handle_starttag(self, tagname, attrs, line):
@@ -95,6 +97,10 @@ def handle_comment(self, comment_text, line):
9597
self.handle_starttag("comment", {"text": comment_text}, line)
9698
self.handle_closingtag("comment", line)
9799

100+
def handle_doctype(self, doctype, line):
101+
self.handle_starttag("doctype", {"text": doctype}, line)
102+
self.handle_closingtag("doctype", line)
103+
98104
def new_element_by_tagname(self, tagname):
99105
"""
100106
Creates an new element based on the tagname.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Deze tekst komt uit de arduino library,
2+
en moet later worden vervangen door C code

HTML_to_C_compiler/utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,10 @@ def file_dir(filepath):
4343

4444
def filename(filepath):
4545
return re.split("\/|\\\\", filepath[::-1], 1)[0][::-1]
46+
47+
#returns the content of a file
48+
def includeFile(filepath):
49+
file = open(filepath, "r")
50+
content = file.read()
51+
file.close()
52+
return content

working-code.c

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,35 @@
1+
Deze tekst komt uit de arduino library,
2+
en moet later worden vervangen door C code
13
// multi-line
24
// comments
35
// work
46
int a = 5;
57
int b = 6;
6-
String eenString = "poep";
8+
String eenString = "multilinestring";
79
char eenChar = 'b';
810
bool timoIsAnAsshole = true;
911
bool hilkoIsAnAsshole = false;
1012
int c = a;
13+
int b = &a;
1114
a = 100;
12-
logic()
13-
14-
15-
int multiplyPoep(int a, int b) {
16-
return a*b;
17-
15+
expression()
16+
break()
17+
// OR?
18+
pls-stop()
19+
continue()
20+
// OR?
21+
next-pls()
22+
23+
24+
int multiply(int a) {
25+
// <param b default=6/>
26+
return a*b;
27+
now-its-your-problem()
1828
}
29+
// 2 ways of including a file:
30+
int includedVar = 4;
31+
// the following import will be ignored because 'working-code.html' imported this file:
32+
// if it's not ignored an infinite loop of imports will occur
1933
multiply(4, 5)
2034

2135

working-code.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
<!DOCTYPE arduino>
22

33

44

0 commit comments

Comments
 (0)