From 311a001a1400bedd0dc005d79f4f83706078e2e2 Mon Sep 17 00:00:00 2001 From: SirOlaf <34164198+SirOlaf@users.noreply.github.com> Date: Sun, 21 Jul 2024 08:55:55 +0200 Subject: [PATCH] Unary negation operator (#21) --- doc/nifc-spec.md | 1 + src/nifc/genexprs.nim | 15 +++------------ src/nifc/nifc_model.nim | 1 + 3 files changed, 5 insertions(+), 12 deletions(-) diff --git a/doc/nifc-spec.md b/doc/nifc-spec.md index 5eb6da0d..ec4d2234 100644 --- a/doc/nifc-spec.md +++ b/doc/nifc-spec.md @@ -94,6 +94,7 @@ Expr ::= Number | CharLiteral | StringLiteral | (and Expr Expr) | # "&&" (or Expr Expr) | # "||" (not Expr) | # "!" + (neg Expr) | (sizeof Expr) | (oconstr Type (kv Symbol Expr)*) | # (object constructor){...} (aconstr Type Expr*) | # array constructor diff --git a/src/nifc/genexprs.nim b/src/nifc/genexprs.nim index 66ce8382..ccbe2c03 100644 --- a/src/nifc/genexprs.nim +++ b/src/nifc/genexprs.nim @@ -63,12 +63,7 @@ proc genLvalue(c: var GeneratedCode; t: Tree; n: NodePos) = let name = mangle(c.m.lits.strings[lit]) c.add name c.requestedSyms.incl name - of DerefC: - let arg = n.firstSon - c.add ParLe - c.add "*" - genx c, t, arg - c.add ParRi + of DerefC: unop "*" of AtC: let (a, i) = sons2(t, n) genx c, t, a @@ -148,12 +143,7 @@ proc genx(c: var GeneratedCode; t: Tree; n: NodePos) = c.add ParLe genx c, t, arg c.add ParRi - of AddrC: - let arg = n.firstSon - c.add ParLe - c.add "&" - genx c, t, arg - c.add ParRi + of AddrC: unop "&" of SizeofC: let arg = n.firstSon c.add "sizeof" @@ -175,6 +165,7 @@ proc genx(c: var GeneratedCode; t: Tree; n: NodePos) = of AndC: cmpop " && " of OrC: cmpop " || " of NotC: unOp " !" + of NegC: unOp " -" of EqC: cmpop " == " of LeC: cmpop " <= " of LtC: cmpop " < " diff --git a/src/nifc/nifc_model.nim b/src/nifc/nifc_model.nim index 3d088aff..7956d3af 100644 --- a/src/nifc/nifc_model.nim +++ b/src/nifc/nifc_model.nim @@ -26,6 +26,7 @@ type AndC = "and" OrC = "or" NotC = "not" + NegC = "neg" SizeofC = "sizeof" OconstrC = "oconstr" AconstrC = "aconstr"