Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ jobs:
- run: go mod tidy
- run: go build -o kvlang ./cmd/kvlang/
- run: go install github.com/array2d/kvspace-go/cmd/kvspace@latest
- run: python3 -m unittest tutorial.test.BenchmarkTest
- run: python3 tutorial/test.py

# ── 多平台交叉编译(仅 tag 时生成 release 产物)─────────────────────────
Expand Down
55 changes: 0 additions & 55 deletions kvspace.go

This file was deleted.

22 changes: 22 additions & 0 deletions tutorial/01-basics/arith.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <math.h>
#include <stdio.h>

int main(void) {
volatile int lhs = 10;
volatile int rhs = 3;
volatile double base = 2.0;
volatile double exponent = 5.0;
volatile double radicand = 144.0;

printf("add: %d\n", lhs + rhs);
printf("sub: %d\n", lhs - rhs);
printf("mul: %d\n", lhs * rhs);
printf("mul(×): %d\n", lhs * rhs);
printf("div: %d\n", lhs / rhs);
printf("div(÷): %d\n", lhs / rhs);
printf("mod: %d\n", lhs % rhs);
printf("pow: %.1f\n", pow(base, exponent));
printf("sqrt: %.1f\n", sqrt(radicand));
printf("sqrt(√): %.1f\n", sqrt(radicand));
return 0;
}
19 changes: 19 additions & 0 deletions tutorial/01-basics/arith.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import math


lhs = 10
rhs = 3
base = 2.0
exponent = 5.0
radicand = 144.0

print("add:", lhs + rhs)
print("sub:", lhs - rhs)
print("mul:", lhs * rhs)
print("mul(×):", lhs * rhs)
print("div:", lhs // rhs)
print("div(÷):", lhs // rhs)
print("mod:", lhs % rhs)
print("pow:", base**exponent)
print("sqrt:", math.sqrt(radicand))
print("sqrt(√):", math.sqrt(radicand))
6 changes: 6 additions & 0 deletions tutorial/01-basics/hello.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <stdio.h>

int main(void) {
puts("hello kvlang");
return 0;
}
1 change: 1 addition & 0 deletions tutorial/01-basics/hello.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("hello kvlang")
15 changes: 15 additions & 0 deletions tutorial/04-algo/factorial.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <stdio.h>

static long long factorial(int n) {
long long result = 1;
for (int i = 1; i <= n; ++i) {
result *= i;
}
return result;
}

int main(void) {
volatile int n = 10;
printf("fact = %lld\n", factorial(n));
return 0;
}
2 changes: 0 additions & 2 deletions tutorial/04-algo/factorial.kv
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ rwfunc factorial(n:int64) -> (result:int64) {
i <- 1
while (i <= n) {
result = result * i # = 等价于 <-
result_m = result × i # = 等价于 <-
i + 1 -> i
}
}

ans <- factorial(10)
println("fact =", ans)
/last_fact = ans # = 等价于 <-
8 changes: 8 additions & 0 deletions tutorial/04-algo/factorial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def factorial(n: int) -> int:
result = 1
for i in range(1, n + 1):
result *= i
return result


print("fact =", factorial(10))
21 changes: 21 additions & 0 deletions tutorial/04-algo/fibonacci.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <stdio.h>

static long long fibonacci(int n) {
if (n <= 1) {
return n;
}
long long a = 0;
long long b = 1;
for (int i = 2; i <= n; ++i) {
long long next = a + b;
a = b;
b = next;
}
return b;
}

int main(void) {
volatile int n = 10;
printf("fib = %lld\n", fibonacci(n));
return 0;
}
9 changes: 4 additions & 5 deletions tutorial/04-algo/fibonacci.kv
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@
# fib = 55
rwfunc fibonacci(n:int64) -> (result:int64) {
if (n <= 1) {
n + 0 -> result
n -> result
} else {
a <- 0
b = 1 # = 等价于 <-
2 -> i
while (i <= n) {
c <- a + b
a = b + 0 # = 等价于 <-
c + 0 -> b
a = b # = 等价于 <-
c -> b
i <- i + 1
}
result = b + 0 # = 等价于 <-
result = b # = 等价于 <-
}
}

fibonacci(10) -> ans
println("fib =", ans)
/last_fib <- ans
10 changes: 10 additions & 0 deletions tutorial/04-algo/fibonacci.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def fibonacci(n: int) -> int:
if n <= 1:
return n
a, b = 0, 1
for _ in range(2, n + 1):
a, b = b, a + b
return b


print("fib =", fibonacci(10))
15 changes: 15 additions & 0 deletions tutorial/04-algo/gcd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <stdio.h>

static long long gcd(long long a, long long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}

int main(void) {
volatile long long a = 48;
volatile long long b = 18;
printf("gcd = %lld\n", gcd(a, b));
return 0;
}
3 changes: 1 addition & 2 deletions tutorial/04-algo/gcd.kv
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# gcd = 6
rwfunc gcd(A:int64, B:int64) -> (R:int64) {
if (B == 0) {
A + 0 -> R
A -> R
} else {
rem <- A % B
R = gcd(B, rem) # = 等价于 <-
Expand All @@ -13,4 +13,3 @@ rwfunc gcd(A:int64, B:int64) -> (R:int64) {

gcd(48, 18) -> ans
println("gcd =", ans)
/last_gcd <- ans
7 changes: 7 additions & 0 deletions tutorial/04-algo/gcd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def gcd(a: int, b: int) -> int:
if b == 0:
return a
return gcd(b, a % b)


print("gcd =", gcd(48, 18))
27 changes: 27 additions & 0 deletions tutorial/04-algo/prime_sieve.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <stdbool.h>
#include <stdio.h>

static void prime_sieve(int limit) {
printf("primes up to %d\n", limit);
int count = 0;
for (int n = 2; n <= limit; ++n) {
bool is_prime = true;
for (int divisor = 2; divisor < n; ++divisor) {
if (n % divisor == 0) {
is_prime = false;
break;
}
}
if (is_prime) {
printf(" prime: %d\n", n);
++count;
}
}
printf("total primes up to %d = %d\n", limit, count);
}

int main(void) {
volatile int limit = 30;
prime_sieve(limit);
return 0;
}
3 changes: 1 addition & 2 deletions tutorial/04-algo/prime_sieve.kv
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ rwfunc prime_sieve(limit:int64) -> () {

if (divisible) {
is_prime = false # = 等价于 <-
n -> d
break
} else {
d <- d + 1
}
Expand All @@ -31,7 +31,6 @@ rwfunc prime_sieve(limit:int64) -> () {
n + 1 -> n
}
println("total primes up to", limit, "=", count)
/last_primes <- count
}

prime_sieve(30)
16 changes: 16 additions & 0 deletions tutorial/04-algo/prime_sieve.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def prime_sieve(limit: int) -> None:
print("primes up to", limit)
count = 0
for n in range(2, limit + 1):
is_prime = True
for divisor in range(2, n):
if n % divisor == 0:
is_prime = False
break
if is_prime:
print(" prime:", n)
count += 1
print("total primes up to", limit, "=", count)


prime_sieve(30)
7 changes: 7 additions & 0 deletions tutorial/benchmark.csv
Comment thread
carsontung666 marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
file,kvlang_ms,python_ms,c_ms
tutorial/01-basics/arith.kv,376.663,25.367,5.123
tutorial/01-basics/hello.kv,86.866,24.346,4.780
tutorial/04-algo/factorial.kv,917.746,25.138,5.087
tutorial/04-algo/fibonacci.kv,1225.605,25.423,5.636
tutorial/04-algo/gcd.kv,729.042,24.436,4.597
tutorial/04-algo/prime_sieve.kv,22567.890,25.147,5.014
Loading
Loading