Skip to content

Commit

Permalink
add shiftleft, more of parse.c building
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewchambers committed Dec 28, 2016
1 parent 9c55f27 commit 575498c
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 14 deletions.
1 change: 1 addition & 0 deletions ast.myr
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ pkg qc =
type bop = union
`Oplus
`Ominus
`Oshl
`Omul
`Odiv
`Omod
Expand Down
4 changes: 2 additions & 2 deletions cpp.myr
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,8 @@ const evalshiftexpr = {expr
l = evaladdexpr(expr)
while true
match expr#[0].kind
| `Tbsl: f = {l, r; -> l << r}
| `Tbsr: f = {l, r; -> l >> r}
| `Tshl: f = {l, r; -> l << r}
| `Tshr: f = {l, r; -> l >> r}
| _:
break
;;
Expand Down
1 change: 1 addition & 0 deletions emit.myr
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,7 @@ const emitbinop2 = {op, ty, lexpr, rexpr
| `Oxor: o = "xor"
| `Oand: o = "and"
| `Oor: o = "or"
| `Oshl: o = "shl"
;;

oi("{} ={} {} {}, {}\n", result, class, o, l, r)
Expand Down
7 changes: 5 additions & 2 deletions fold.myr
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@ const foldexpr = {e
;;
| &(`Ebinop [.op=op, .l=l, .r=r]):
match (op, foldexpr(l), foldexpr(r))
| (`Oplus, `std.Some `Constint c1, `std.Some `Constint c2):
-> `std.Some `Constint c1 + c2
| (`Oshl, `std.Some `Constint c1, `std.Some `Constint c2):
-> `std.Some `Constint c1 << c2
| (_, `std.None, _):
-> `std.None
| (_, _, `std.None):
-> `std.None
| (`Oplus, `std.Some `Constint c1, `std.Some `Constint c2):
-> `std.Some `Constint c1 + c2
| _:
-> `std.None
;;
| _:
-> `std.None
Expand Down
22 changes: 20 additions & 2 deletions parse.myr
Original file line number Diff line number Diff line change
Expand Up @@ -1897,7 +1897,7 @@ const parseeqlexpr = {p
const parserelexpr = {p
var l, r, op

l = parseaddexpr(p)
l = parseshiftexpr(p)
while true
match p.tok.kind
| `Tgt: op = `Ogt
Expand All @@ -1908,12 +1908,30 @@ const parserelexpr = {p
break
;;
next(p)
r = parseaddexpr(p)
r = parseshiftexpr(p)
l = mkrelop(op, l, r)
;;
-> l
}

const parseshiftexpr = {p
var l, r, op

l = parseaddexpr(p)
while true
match p.tok.kind
| `Tshl: op = `Oshl
// | `Tshr: op = `Osr
| _:
break
;;
next(p)
r = parseaddexpr(p)
l = mkbinop(op, l, r)
;;
-> l
}

const parseaddexpr = {p
var l, r, op

Expand Down
12 changes: 12 additions & 0 deletions test/execute/0104-shl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

int
main()
{
int x;

x = 1;
if ((x << 1) != 2)
return 1;

return 0;
}
17 changes: 9 additions & 8 deletions tok.myr
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,17 @@ pkg qc =
`Tboreq /* |= */
`Tbxoreq /* ^= */
`Tbandeq /* &= */
`Tbsleq /* <<= */
`Tbsreq /* >>= */
`Tshleq /* <<= */
`Tshreq /* >>= */

`Tbor /* | */
`Tbxor /* ^ */
`Tband /* & */
`Tbsl /* << */
`Tbsr /* >> */
`Tbnot /* ~ */

`Tshl /* << */
`Tshr /* >> */

`Teq /* == */
`Tgt /* > */
`Tlt /* < */
Expand Down Expand Up @@ -638,9 +639,9 @@ const oper = {ts
t = `Tle;
elif matchc(ts, '<')
if matchc(ts, '=')
t = `Tbsleq
t = `Tshleq
else
t = `Tbsl
t = `Tshl
;;
else
t = `Tlt;
Expand All @@ -650,9 +651,9 @@ const oper = {ts
t = `Tge
elif matchc(ts, '>')
if matchc(ts, '=')
t = `Tbsreq
t = `Tshreq
else
t = `Tbsr
t = `Tshr
;;
else
t = `Tgt;
Expand Down

0 comments on commit 575498c

Please sign in to comment.