Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
G
GWFHLang
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
GWFHLang
Commits
8af25700
Kaydet (Commit)
8af25700
authored
Mar 26, 2019
tarafından
Batuhan Taşkaya
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
(Literal | Symbol) vs (Literal | Symbol) comparsion
üst
43c1caf8
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
86 additions
and
52 deletions
+86
-52
compiler.py
gwfhlang/compiler.py
+77
-49
grammar.lark
gwfhlang/grammar.lark
+9
-3
No files found.
gwfhlang/compiler.py
Dosyayı görüntüle @
8af25700
...
...
@@ -8,22 +8,19 @@ from arkhe.debugger import ADB
from
itertools
import
chain
from
textwrap
import
dedent
def
arkhe_int
(
value
):
dc
=
(
value
>>
8
)
dc
=
value
>>
8
do
=
value
-
(
dc
<<
8
)
return
dc
,
do
def
name
(
value
):
return
list
(
map
(
ord
,
value
))
COMP_MAP
=
{
"=="
:
"eq"
,
"!="
:
"ne"
,
">"
:
"gt"
,
"<"
:
"lt"
,
">="
:
"ge"
,
"<="
:
"le"
}
COMP_MAP
=
{
"=="
:
"eq"
,
"!="
:
"ne"
,
">"
:
"gt"
,
"<"
:
"lt"
,
">="
:
"ge"
,
"<="
:
"le"
}
"""
Register Reserves;
0, 1 => LOAD
...
...
@@ -32,76 +29,107 @@ Register Reserves;
0..31 => SYMREAD
"""
class
Compiler
(
Transformer
):
def
main
(
self
,
tokens
):
return
list
(
chain
.
from_iterable
(
filter
(
lambda
token
:
token
,
tokens
)))
def
symset
(
self
,
tokens
):
sym
,
val
=
tokens
value
=
literal_eval
(
val
)
if
val
.
type
==
'INT'
:
ops
=
arkhe_int
(
value
)
elif
val
.
type
==
'STR'
:
ops
=
list
(
map
(
ord
,
value
))
ops
.
append
(
TypeTable
.
STR
.
value
)
elif
val
.
type
==
'BYT'
:
ops
=
list
(
map
(
ord
,
value
.
decode
(
'utf8'
)))
ops
.
append
(
TypeTable
.
BYT
.
value
)
loads
=
[
*
create_instr
(
"load"
,
0
,
*
name
(
sym
),
TypeTable
.
STR
.
value
),
*
create_instr
(
"load"
,
1
,
*
ops
)]
ops
=
self
.
load_value
(
val
)
loads
=
[
*
create_instr
(
"load"
,
0
,
*
name
(
sym
),
TypeTable
.
STR
.
value
),
*
create_instr
(
"load"
,
1
,
*
ops
),
]
return
[
*
loads
,
*
create_instr
(
"symset"
,
0
,
1
)]
def
comp
(
self
,
tokens
):
op1
,
comp
,
op2
=
tokens
comp
=
COMP_MAP
.
get
(
comp
.
children
[
0
]
.
value
,
"eq"
)
comp
=
COMP_MAP
.
get
(
tokens
.
pop
(
1
)
.
children
[
0
]
.
value
,
"eq"
)
read
,
regs
=
self
.
symread
(
op1
,
op2
)
return
[
*
read
,
*
create_instr
(
comp
,
*
regs
)]
inst
=
[]
regs
=
[]
for
operand
in
tokens
:
if
operand
.
type
==
'SYM'
:
read
,
reg
=
self
.
symread
(
str
(
operand
))
inst
.
extend
(
read
)
regs
.
extend
(
reg
)
elif
operand
.
type
==
'STR'
or
operand
.
type
==
'INT'
:
read
,
reg
=
self
.
load_value
(
operand
),
max
(
regs
,
default
=
0
)
+
1
inst
.
extend
(
create_instr
(
"load"
,
reg
,
*
read
))
regs
.
append
(
reg
)
return
[
*
inst
,
*
create_instr
(
comp
,
*
regs
)]
def
symread
(
self
,
*
syms
):
loads
=
[
create_instr
(
"load"
,
n
,
*
name
(
item
),
TypeTable
.
STR
.
value
)
for
n
,
item
in
enumerate
(
syms
)]
loads
=
[
create_instr
(
"load"
,
n
,
*
name
(
item
),
TypeTable
.
STR
.
value
)
for
n
,
item
in
enumerate
(
syms
)
]
symreads
=
[
create_instr
(
"symread"
,
n
,
n
)
for
n
in
range
(
len
(
syms
))]
return
list
(
chain
.
from_iterable
([
*
loads
,
*
symreads
])),
list
(
range
(
len
(
syms
)))
def
if_stmt
(
self
,
tokens
):
if
len
(
tokens
)
==
3
:
comp
,
suite
,
suite_else
=
tokens
loads
=
[
*
create_instr
(
"load"
,
29
,
*
arkhe_int
(
len
(
suite
))),
*
create_instr
(
"load"
,
30
,
*
arkhe_int
(
len
(
suite_else
))),
*
create_instr
(
"load"
,
31
,
*
arkhe_int
(
0
))]
loads
=
[
*
create_instr
(
"load"
,
29
,
*
arkhe_int
(
len
(
suite
))),
*
create_instr
(
"load"
,
30
,
*
arkhe_int
(
len
(
suite_else
))),
*
create_instr
(
"load"
,
31
,
*
arkhe_int
(
0
)),
]
code
=
[
*
loads
,
*
comp
,
*
create_instr
(
'jfe'
,
31
),
# No jump
*
create_instr
(
'jfn'
,
29
),
# Jump amount of suite
*
create_instr
(
"jfe"
,
31
),
# No jump
*
create_instr
(
"jfn"
,
29
),
# Jump amount of suite
*
suite
,
*
create_instr
(
'jfn'
,
31
),
*
create_instr
(
'jfe'
,
30
),
*
suite_else
*
create_instr
(
"jfn"
,
31
),
*
create_instr
(
"jfe"
,
30
),
*
suite_else
,
]
else
:
comp
,
suite
=
tokens
loads
=
[
*
create_instr
(
"load"
,
29
,
*
arkhe_int
(
len
(
suite
))),
*
create_instr
(
"load"
,
30
,
*
arkhe_int
(
0
))]
loads
=
[
*
create_instr
(
"load"
,
29
,
*
arkhe_int
(
len
(
suite
))),
*
create_instr
(
"load"
,
30
,
*
arkhe_int
(
0
)),
]
code
=
[
*
loads
,
*
comp
,
*
create_instr
(
'jfe'
,
30
),
# No jump
*
create_instr
(
'jfn'
,
29
),
# Jump amount of suite
*
create_instr
(
"jfe"
,
30
),
# No jump
*
create_instr
(
"jfn"
,
29
),
# Jump amount of suite
*
suite
,
]
return
code
def
suite
(
self
,
tokens
):
return
list
(
chain
.
from_iterable
(
tokens
))
def
load_value
(
self
,
val
):
value
=
literal_eval
(
val
)
if
val
.
type
==
"INT"
:
ops
=
arkhe_int
(
value
)
elif
val
.
type
==
"STR"
:
ops
=
list
(
map
(
ord
,
value
))
ops
.
append
(
TypeTable
.
STR
.
value
)
elif
val
.
type
==
"BYT"
:
ops
=
list
(
map
(
ord
,
value
.
decode
(
"utf8"
)))
ops
.
append
(
TypeTable
.
BYT
.
value
)
return
ops
def
ccall
(
self
,
tokens
):
func
,
*
args
=
tokens
args
,
args_regs
=
self
.
symread
(
*
args
)
return
[
*
args
,
*
create_instr
(
"load"
,
27
,
*
name
(
str
(
func
)),
TypeTable
.
STR
.
value
),
*
create_instr
(
"ccall"
,
27
,
*
args_regs
,
28
)]
return
[
*
args
,
*
create_instr
(
"load"
,
27
,
*
name
(
str
(
func
)),
TypeTable
.
STR
.
value
),
*
create_instr
(
"ccall"
,
27
,
*
args_regs
,
28
),
]
gwfhlang/grammar.lark
Dosyayı görüntüle @
8af25700
...
...
@@ -5,11 +5,17 @@ if_stmt: "if" comp ":" suite ["else" ":" suite]
suite: _NEWLINE _INDENT expr+ _DEDENT
?expr: (symset | comp | ccall) _NEWLINE
symset: SYM "=" (INT | STR)
comp: SYM op SYM
ccall: SYM "(" SYM? ("," SYM)* ")"
symset: SYM "=" (INT | STR | arith)
comp: (INT | STR | SYM) op (INT | STR | SYM)
arith: (INT | STR | SYM) fact (INT | STR | SYM)
call: SYM "(" SYM? ("," SYM)* ")"
ccall: "!" SYM "(" SYM? ("," SYM)* ")"
!op: "<" | ">" | "=" | ">=" | "<=" | "!="
!fact: "+" | "-" | "*" | "/"
COMMENT: /#[^\n]*/
_NEWLINE: ( /\r?\n[\t ]*/ | COMMENT )+
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment