Skip to content

Commit

Permalink
wip nep25
Browse files Browse the repository at this point in the history
  • Loading branch information
ghewgill committed Feb 8, 2024
1 parent acd44b1 commit 8d4c229
Show file tree
Hide file tree
Showing 22 changed files with 186 additions and 172 deletions.
2 changes: 1 addition & 1 deletion lib/base.neon
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ EXPORT FUNCTION from(base: Number, digits: String): NumberResult
CHECK d ISA string.FindResult.index ELSE
RETURN NumberResult.error("invalid digit: \(c) at index \(i)")
END CHECK
r := r * base + d.index
r := (r * base) + d.index
END FOREACH
RETURN NumberResult.value(r)
END FUNCTION
Expand Down
4 changes: 2 additions & 2 deletions lib/bigint.neon
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ FUNCTION sub(x, y: BigInt): BigInt
VAR i: Number := 0
VAR borrow: Number := 0
WHILE i < x.digits.size() DO
r.digits[i] := (IF i < x.digits.size() THEN x.digits[i] ELSE 0) - (IF i < y.digits.size() THEN y.digits[i] ELSE 0) - borrow
r.digits[i] := ((IF i < x.digits.size() THEN x.digits[i] ELSE 0) - (IF i < y.digits.size() THEN y.digits[i] ELSE 0)) - borrow
IF r.digits[i] < 0 THEN
r.digits[i] := r.digits[i] + Base
borrow := 1
Expand Down Expand Up @@ -216,7 +216,7 @@ FUNCTION mul(x, y: BigInt): BigInt
VAR p: BigInt := Zero
VAR carry: Number := 0
FOR j := 0 TO x.digits.size()-1 DO
LET z: Number := (IF j < x.digits.size() THEN x.digits[j] ELSE 0) * y.digits[i] + carry
LET z: Number := ((IF j < x.digits.size() THEN x.digits[j] ELSE 0) * y.digits[i]) + carry
p.digits[i+j] := z MOD Base
carry := math.floor(z / Base)
END FOR
Expand Down
26 changes: 13 additions & 13 deletions lib/complex.neon
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ FUNCTION acos(a: Complex): Complex
LET s2: Complex := make(1 + a.re, a.im).sqrt()
LET r1: Number := 2 * math.atan2(s1.re, s2.re)
VAR i1: Number := (s2.re*s1.im) - (s2.im*s1.re)
i1 := sign(i1) * math.log(math.abs(i1) + math.sqrt(i1*i1 + 1))
i1 := sign(i1) * math.log(math.abs(i1) + math.sqrt((i1*i1) + 1))
RETURN make(r1, i1)
END FUNCTION

Expand All @@ -115,7 +115,7 @@ FUNCTION acosh(a: Complex): Complex
LET s1: Complex := make(a.re - 1, a.im).sqrt()
LET s2: Complex := make(a.re + 1, a.im).sqrt()
VAR r1: Number := (s1.re*s2.re) + (s1.im*s2.im)
r1 := sign(r1) * math.log(math.abs(r1) + math.sqrt(r1*r1 + 1))
r1 := sign(r1) * math.log(math.abs(r1) + math.sqrt((r1*r1) + 1))
LET i1: Number := 2 * math.atan2(s1.im, s2.re)
RETURN make(r1, i1)
END FUNCTION
Expand All @@ -128,7 +128,7 @@ END FUNCTION
* http://mathworld.wolfram.com/AbsoluteValue.html
*/
FUNCTION abs(a: Complex): Number
RETURN math.sqrt(a.re^2 + a.im^2)
RETURN math.sqrt((a.re^2) + (a.im^2))
END FUNCTION

/* Function: add
Expand Down Expand Up @@ -164,7 +164,7 @@ FUNCTION asin(a: Complex): Complex
LET s1: Complex := make(1 + a.re, a.im).sqrt()
LET s2: Complex := make(1 - a.re, -a.im).sqrt()
VAR r1: Number := (s1.re*s2.im) - (s2.re*s1.im)
r1 := sign(r1) * math.log(math.abs(r1) + math.sqrt(r1*r1 + 1))
r1 := sign(r1) * math.log(math.abs(r1) + math.sqrt((r1*r1) + 1))
LET i1: Number := math.atan2(a.re, (s1.re*s2.re) - (s1.im*s2.im))
RETURN make(i1, -r1)
END FUNCTION
Expand All @@ -180,7 +180,7 @@ FUNCTION asinh(a: Complex): Complex
LET s1: Complex := make(1 + a.im, -a.re).sqrt()
LET s2: Complex := make(1 - a.im, a.re).sqrt()
VAR r1: Number := (s1.re * s2.im) - (s2.re * s1.im)
r1 := sign(r1) * math.log(math.abs(r1) + math.sqrt(r1 * r1 + 1))
r1 := sign(r1) * math.log(math.abs(r1) + math.sqrt((r1 * r1) + 1))
LET i1: Number := math.atan2(a.im, (s1.re * s2.re) - (s1.im * s2.im))
RETURN make(r1, i1)
END FUNCTION
Expand Down Expand Up @@ -250,8 +250,8 @@ END FUNCTION
* http://mathworld.wolfram.com/ComplexDivision.html
*/
FUNCTION div(a, b: Complex): Complex
LET d: Number := b.re^2 + b.im^2
RETURN make((a.re*b.re + a.im*b.im) / d, (a.im*b.re - a.re*b.im) / d)
LET d: Number := (b.re^2) + (b.im^2)
RETURN make(((a.re*b.re) + (a.im*b.im)) / d, ((a.im*b.re) - (a.re*b.im)) / d)
END FUNCTION

/* Function: exp
Expand Down Expand Up @@ -287,7 +287,7 @@ END FUNCTION
* http://mathworld.wolfram.com/MultiplicativeInverse.html
*/
FUNCTION inv(a: Complex): Complex
LET d: Number := a.re*a.re + a.im*a.im
LET d: Number := (a.re*a.re) + (a.im*a.im)
RETURN make(a.re/d, -a.im/d)
END FUNCTION

Expand All @@ -299,7 +299,7 @@ END FUNCTION
* http://mathworld.wolfram.com/Logarithm.html
*/
FUNCTION log(a: Complex): Complex
RETURN make(math.log(a.re*a.re + a.im*a.im)/2, a.arg())
RETURN make(math.log((a.re*a.re) + (a.im*a.im))/2, a.arg())
END FUNCTION

/* Function: log10
Expand All @@ -322,7 +322,7 @@ END FUNCTION
* http://mathworld.wolfram.com/ComplexMultiplication.html
*/
FUNCTION mul(a, b: Complex): Complex
RETURN make(a.re * b.re - a.im * b.im, a.re * b.im + a.im * b.re)
RETURN make((a.re * b.re) - (a.im * b.im), (a.re * b.im) + (a.im * b.re))
END FUNCTION

/* Function: pow
Expand All @@ -335,8 +335,8 @@ END FUNCTION
FUNCTION pow(a, b: Complex): Complex
LET p: Number := a.arg()
LET m: Number := a.abs()
LET r: Number := m^b.re * math.exp(-b.im * p)
LET t: Number := b.re * p + b.im * math.log(m)
LET r: Number := (m^b.re) * math.exp(-b.im * p)
LET t: Number := (b.re * p) + (b.im * math.log(m))
RETURN make(r * math.cos(t), r * math.sin(t))
END FUNCTION

Expand Down Expand Up @@ -390,7 +390,7 @@ END FUNCTION
* Complex square (a^2).
*/
FUNCTION square(a: Complex): Complex
RETURN make(a.re*a.re - a.im*a.im, 2*a.re*a.im)
RETURN make((a.re*a.re) - (a.im*a.im), 2*a.re*a.im)
END FUNCTION

/* Function: tan
Expand Down
6 changes: 3 additions & 3 deletions lib/datetime.neon
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ FUNCTION makeFromInstant(inst: Number): DateTime
year WITH 1900 + tm.tm_year,
month WITH 1 + tm.tm_mon,
day WITH tm.tm_mday,
weekday WITH 1 + (tm.tm_wday + 6) MOD 7,
weekday WITH 1 + ((tm.tm_wday + 6) MOD 7),
hour WITH tm.tm_hour,
minute WITH tm.tm_min,
second WITH tm.tm_sec,
Expand Down Expand Up @@ -256,10 +256,10 @@ END FUNCTION
FUNCTION DateTime.plusPeriod(self: DateTime, period: Period): DateTime
VAR dt: DateTime := self
dt.year := dt.year + period.years
LET m: Number := dt.month - 1 + period.months
LET m: Number := (dt.month - 1) + period.months
dt.year := dt.year + math.floor(m / 12)
dt.month := 1 + (m MOD 12)
RETURN makeFromParts(dt).plusDuration(86400*period.days + 3600*period.hours + 60*period.minutes + period.seconds)
RETURN makeFromParts(dt).plusDuration((86400*period.days) + (3600*period.hours) + (60*period.minutes) + period.seconds)
END FUNCTION

/* Function: DateTime.toString
Expand Down
6 changes: 3 additions & 3 deletions lib/json.neon
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ FUNCTION decodePart(json: String, INOUT index: Number): JsonResult
VAR char: Number := 0
FOR i := 1 TO 4 DO
IF "0" <= json[index+i] <= "9" THEN
char := char * 0x10 + (string.toCodePoint(json[index+i]) - string.toCodePoint("0"))
char := (char * 0x10) + (string.toCodePoint(json[index+i]) - string.toCodePoint("0"))
ELSIF "a" <= json[index+i] <= "f" THEN
char := char * 0x10 + (string.toCodePoint(json[index+i]) - string.toCodePoint("a"))
char := (char * 0x10) + (string.toCodePoint(json[index+i]) - string.toCodePoint("a"))
ELSIF "A" <= json[index+i] <= "F" THEN
char := char * 0x10 + (string.toCodePoint(json[index+i]) - string.toCodePoint("A"))
char := (char * 0x10) + (string.toCodePoint(json[index+i]) - string.toCodePoint("A"))
ELSE
RETURN JsonResult.error(DecodeError(message WITH "invalid hex character", index WITH index+i))
END IF
Expand Down
2 changes: 1 addition & 1 deletion lib/regex.neon
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ FUNCTION ExprGroup.compile(self: VALID POINTER TO ExprGroup, INOUT regex: Regex)
IF self->capture THEN
regex.append(Opcode.save(2*self->index))
self->expr->compile(INOUT regex)
regex.append(Opcode.save(2*self->index+1))
regex.append(Opcode.save((2*self->index)+1))
ELSE
self->expr->compile(INOUT regex)
END IF
Expand Down
6 changes: 3 additions & 3 deletions lib/string.neon
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ FUNCTION format(s: String, fmt: String): String
ELSIF align IN [">", "="] THEN
r := repeat(fill, space) & r
ELSIF align = "^" THEN
r := repeat(fill, space INTDIV 2) & r & repeat(fill, space - space INTDIV 2)
r := repeat(fill, space INTDIV 2) & r & repeat(fill, space - (space INTDIV 2))
END IF
END IF
END IF
Expand Down Expand Up @@ -220,7 +220,7 @@ FUNCTION padLeft(s: String, pad: String, width: Number): String
CHECK n > 0 ELSE
RETURN s
END CHECK
RETURN repeat(pad, n INTDIV pad.length()) & pad[0 TO n MOD pad.length() - 1] & s
RETURN repeat(pad, n INTDIV pad.length()) & pad[0 TO (n MOD pad.length()) - 1] & s
END FUNCTION

/*
Expand All @@ -234,7 +234,7 @@ FUNCTION padRight(s: String, pad: String, width: Number): String
CHECK n > 0 ELSE
RETURN s
END CHECK
RETURN s & pad[LAST - (n MOD pad.length() - 1) TO LAST] & repeat(pad, n INTDIV pad.length())
RETURN s & pad[LAST - ((n MOD pad.length()) - 1) TO LAST] & repeat(pad, n INTDIV pad.length())
END FUNCTION

/* Function: quoted
Expand Down
6 changes: 3 additions & 3 deletions neon/lexer.neon
Original file line number Diff line number Diff line change
Expand Up @@ -474,12 +474,12 @@ FUNCTION tokenize_fragment(source_path: String, INOUT line: Number, column_start
EXIT LOOP
END IF
IF d <> "_" THEN
LET x: Number := (IF "0" <= d <= "9" THEN string.toCodePoint(d) - string.toCodePoint("0") ELSE (IF "a" <= d <= "z" THEN string.toCodePoint(d) - string.toCodePoint("a") + 10 ELSE -1))
LET x: Number := (IF "0" <= d <= "9" THEN string.toCodePoint(d) - string.toCodePoint("0") ELSE (IF "a" <= d <= "z" THEN (string.toCodePoint(d) - string.toCodePoint("a")) + 10 ELSE -1))
IF x < 0 OR x >= base THEN
error(1005, t, "invalid digit for given base")
END IF
-- TODO decimal
value := value * base + x
value := (value * base) + x
END IF
INC i
END LOOP
Expand Down Expand Up @@ -586,7 +586,7 @@ FUNCTION tokenize_fragment(source_path: String, INOUT line: Number, column_start
IF NOT hex_char(d) THEN
error(1013, t, "invalid hex character")
END IF
x := x * 16 + (IF "0" <= d <= "9" THEN string.toCodePoint(d) - string.toCodePoint("0") ELSE (IF "a" <= d <= "z" THEN string.toCodePoint(d) - string.toCodePoint("a") + 10 ELSE -1))
x := (x * 16) + (IF "0" <= d <= "9" THEN string.toCodePoint(d) - string.toCodePoint("0") ELSE (IF "a" <= d <= "z" THEN (string.toCodePoint(d) - string.toCodePoint("a")) + 10 ELSE -1))
END FOR
c := string.fromCodePoint(x)
i := i + len
Expand Down
4 changes: 2 additions & 2 deletions samples/benchmark/dhrystone.neon
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ FUNCTION main()
Bool_Glob := NOT Func_2 (Str_1_Loc, Str_2_Loc)
-- Bool_Glob == 1
WHILE Int_1_Loc < Int_2_Loc DO -- loop body executed once
Int_3_Loc := 5 * Int_1_Loc - Int_2_Loc
Int_3_Loc := (5 * Int_1_Loc) - Int_2_Loc
-- Int_3_Loc == 7
Proc_7 (Int_1_Loc, Int_2_Loc, OUT Int_3_Loc)
-- Int_3_Loc == 7
Expand All @@ -715,7 +715,7 @@ FUNCTION main()
-- Int_1_Loc == 3, Int_2_Loc == 3, Int_3_Loc == 7
Int_2_Loc := Int_2_Loc * Int_1_Loc
Int_1_Loc := math.floor(Int_2_Loc / Int_3_Loc)
Int_2_Loc := 7 * (Int_2_Loc - Int_3_Loc) - Int_1_Loc
Int_2_Loc := (7 * (Int_2_Loc - Int_3_Loc)) - Int_1_Loc
-- Int_1_Loc == 1, Int_2_Loc == 13, Int_3_Loc == 7
Proc_2 (INOUT Int_1_Loc)
-- Int_1_Loc == 5
Expand Down
16 changes: 8 additions & 8 deletions samples/benchmark/whetstone.neon
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ FUNCTION pa(INOUT e: Array<Number>)
j := 0
WHILE j < 6 DO
e[1] := ( e[1] + e[2] + e[3] - e[4]) * t
e[2] := ( e[1] + e[2] - e[3] + e[4]) * t
e[3] := ( e[1] - e[2] + e[3] + e[4]) * t
e[2] := ((e[1] + e[2] - e[3])+ e[4]) * t
e[3] := ((e[1] - e[2])+ e[3] + e[4]) * t
e[4] := (-e[1] + e[2] + e[3] + e[4]) / T2
j := j + 1
END WHILE
Expand Down Expand Up @@ -189,8 +189,8 @@ C

FOR i := 1 TO N1 DO
X1 := (X1 + X2 + X3 - X4) * t
X2 := (X1 + X2 - X3 + X4) * t
X3 := (X1 - X2 + X3 + X4) * t
X2 := ((X1 + X2 - X3) + X4) * t
X3 := ((X1 - X2) + X3 + X4) * t
X4 := (-X1+ X2 + X3 + X4) * t
END FOR
--#ifdef PRINTOUT
Expand All @@ -209,8 +209,8 @@ C

FOR i := 1 TO N2 DO
E1[1] := ( E1[1] + E1[2] + E1[3] - E1[4]) * t
E1[2] := ( E1[1] + E1[2] - E1[3] + E1[4]) * t
E1[3] := ( E1[1] - E1[2] + E1[3] + E1[4]) * t
E1[2] := ((E1[1] + E1[2] - E1[3])+ E1[4]) * t
E1[3] := ((E1[1] - E1[2])+ E1[3] + E1[4]) * t
E1[4] := (-E1[1] + E1[2] + E1[3] + E1[4]) * t
END FOR

Expand Down Expand Up @@ -274,7 +274,7 @@ C

FOR i := 1 TO N6 DO
j := j * (k-j) * (l-k)
k := l * k - (l-j) * k
k := (l * k) - ((l-j) * k)
l := (l-k) * (k+j)
E1[l-1] := j + k + l
E1[k-1] := j * k * l
Expand Down Expand Up @@ -350,7 +350,7 @@ C
j := j + k
k := j + k
j := k - j
k := k - j - j
k := (k - j) - j
END FOR

--#ifdef PRINTOUT
Expand Down
2 changes: 1 addition & 1 deletion samples/cal/cal.neon
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ FUNCTION main(args: Array<String>)
FOR line := 0 TO 7 DO
VAR s: String := ""
FOR col := 0 TO 2 DO
s.append(months[3*row+col][line] & " ")
s.append(months[(3*row)+col][line] & " ")
END FOR
print(s)
END FOR
Expand Down
2 changes: 1 addition & 1 deletion samples/editor/editor.neon
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ FUNCTION highlight(s: String): String
ELSE
EXIT WHILE
END IF
r[i+m.match[n].first TO i+m.match[n].last] := string.repeat(h, m.match[n].last-m.match[n].first+1)
r[i+m.match[n].first TO i+m.match[n].last] := string.repeat(h, (m.match[n].last-m.match[n].first)+1)
i := i + m.match[n].last + 1
END WHILE
RETURN r
Expand Down
Loading

0 comments on commit 8d4c229

Please sign in to comment.