Skip to content

Commit 6c1325e

Browse files
authored
Merge pull request #303 from taisukef/c--
add C--
2 parents 829213d + c669f08 commit 6c1325e

File tree

6 files changed

+257
-42
lines changed

6 files changed

+257
-42
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
*.class
22
__pycache__
33
a.out
4+
a.out.*
45
*.exe
56
main
67
*.o

README.md

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ Geo3x3
55

66
## Supported Languages
77

8-
109 languages supported ([langlist.csv](langlist.csv) / [langlist.html](https://taisukef.github.io/Geo3x3/langlist.html))
8+
110 languages supported ([langlist.csv](langlist.csv) / [langlist.html](https://taisukef.github.io/Geo3x3/langlist.html))
99

10-
([JavaScript](#in-JavaScript-HTML) / [TypeScript](#in-TypeScript-Deno) / [Zen](#in-Zen) / [C](#in-C) / [C++](#in-C-1) / [C#](#in-C-2) / [Swift](#in-Swift) / [Java](#in-Java) / [Python](#in-Python) / [Ruby](#in-Ruby) / [PHP](#in-PHP) / [Go](#in-Go) / [Kotlin](#in-Kotlin) / [Dart](#in-Dart) / [Rust](#in-Rust) / [Haskell](#in-haskell-ghc-84x-or-later) / [OpenVBS](#in-OpenVBS) / [VBScript](#in-VBScript) / [Visual Basic](#in-Visual-Basic) / [Scala](#in-Scala) / [R](#in-R) / [GAS](#in-GAS-Google-App-Script) / [Nim](#in-Nim) / [Lua](#in-Lua) / [Perl](#in-Perl) / [Elixir](#in-Elixir) / [Groovy](#in-Groovy) / [D](#in-D) / [Julia](#in-Julia) / [Racket](#in-Racket) / [OCaml](#in-OCaml) / [Erlang](#in-Erlang) / [Clojure](#in-Clojure) / [F#](#in-F) / [Haxe](#in-Haxe) / [Scheme](#in-Scheme-R6RS) / [Common Lisp](#in-Common-Lisp) / [Elm](#in-Elm) / [Hack](#in-Hack) / [PureScript](#in-PureScript) / [CoffeeScript](#in-CoffeeScript) / [Objective-C](#in-Objective-C) / [Frege](#in-Frege) / [Eiffel](#in-Eiffel) / [Ada](#in-Ada) / [Free Pascal](#in-Free-Pascal) / [Crystal](#in-Crystal) / [Forth](#in-Forth) / [Bash](#in-Bash) / [AWK](#in-AWK) / [Vim script](#in-Vim-script) / [Vim9 script](#in-Vim9-script) / [IchigoJam BASIC](#in-IchigoJam-BASIC) / [GnuCOBOL](#in-GnuCOBOL) / [MoonScript](#in-MoonScript) / [Octave](#in-Octave) / [Emacs Lisp](#in-Emacs-Lisp) / [Fortran 90](#in-Fortran-90) / [MariaDB SQL/PSM](#in-MariaDB-SQL) / [PL/pgSQL](#in-PLpgSQL) / [Tcl](#in-Tcl) / [V](#in-V) / [Pike](#in-Pike) / [Io](#in-Io) / [Wren](#in-Wren) / [GNU Smalltalk](#in-GNU-Smalltalk) / [JScript](#in-JScript) / [Pharo](#in-Pharo) / [Scratch](#in-Scratch) / [Standard ML](#in-Standard-ML) / [なでしこ](#in-なでしこ) / [Kuin](#in-Kuin) / [ClojureScript](#in-ClojureScript) / [HSP](#in-HSP) / [Reason](#in-Reason) / [THPL](#in-THPL) / [Janet](#in-Janet) / [Phel](#in-Phel) / [Raku](#in-Raku) / [文言](#in-文言) / [Vala](#in-Vala) / [SmileBASIC](#in-SmileBASIC) / [Small Basic](#in-Small-Basic) / [Flix](#in-Flix) / [PowerShell](#in-PowerShell) / [Koka](#in-Koka) / [Zig](#in-Zig) / [BanchaScript](#in-BanchaScript) / [AssemblyScript](#in-AssemblyScript) / [Clawn](#in-Clawn) / [FORTRAN 77](#in-FORTRAN-77) / [orelang](#in-orelang) / [Laze](#in-Laze) / [WebAssembly](#in-WebAssembly) / [Nelua](#in-Nelua) / [Roland](#in-Roland) / [ReScript](#in-ReScript) / [Effekt](#in-Effekt) / [Ceylon](#in-Ceylon) / [Mochi](#in-Mochi) / [Neko](#in-Neko) / [NekoML](#in-NekoML) / [LLVM IR](#in-LLVM-IR) / [Mojo](#in-Mojo) / [Pkl](#in-Pkl) / [Jsonnet](#in-Jsonnet) / [AppleScript](#in-AppleScript) / [DNCL3](#in-DNCL3) / [Wirth](#in-Wirth))
10+
([JavaScript](#in-JavaScript-HTML) / [TypeScript](#in-TypeScript-Deno) / [Zen](#in-Zen) / [C](#in-C) / [C++](#in-C-1) / [C#](#in-C-2) / [Swift](#in-Swift) / [Java](#in-Java) / [Python](#in-Python) / [Ruby](#in-Ruby) / [PHP](#in-PHP) / [Go](#in-Go) / [Kotlin](#in-Kotlin) / [Dart](#in-Dart) / [Rust](#in-Rust) / [Haskell](#in-haskell-ghc-84x-or-later) / [OpenVBS](#in-OpenVBS) / [VBScript](#in-VBScript) / [Visual Basic](#in-Visual-Basic) / [Scala](#in-Scala) / [R](#in-R) / [GAS](#in-GAS-Google-App-Script) / [Nim](#in-Nim) / [Lua](#in-Lua) / [Perl](#in-Perl) / [Elixir](#in-Elixir) / [Groovy](#in-Groovy) / [D](#in-D) / [Julia](#in-Julia) / [Racket](#in-Racket) / [OCaml](#in-OCaml) / [Erlang](#in-Erlang) / [Clojure](#in-Clojure) / [F#](#in-F) / [Haxe](#in-Haxe) / [Scheme](#in-Scheme-R6RS) / [Common Lisp](#in-Common-Lisp) / [Elm](#in-Elm) / [Hack](#in-Hack) / [PureScript](#in-PureScript) / [CoffeeScript](#in-CoffeeScript) / [Objective-C](#in-Objective-C) / [Frege](#in-Frege) / [Eiffel](#in-Eiffel) / [Ada](#in-Ada) / [Free Pascal](#in-Free-Pascal) / [Crystal](#in-Crystal) / [Forth](#in-Forth) / [Bash](#in-Bash) / [AWK](#in-AWK) / [Vim script](#in-Vim-script) / [Vim9 script](#in-Vim9-script) / [IchigoJam BASIC](#in-IchigoJam-BASIC) / [GnuCOBOL](#in-GnuCOBOL) / [MoonScript](#in-MoonScript) / [Octave](#in-Octave) / [Emacs Lisp](#in-Emacs-Lisp) / [Fortran 90](#in-Fortran-90) / [MariaDB SQL/PSM](#in-MariaDB-SQL) / [PL/pgSQL](#in-PLpgSQL) / [Tcl](#in-Tcl) / [V](#in-V) / [Pike](#in-Pike) / [Io](#in-Io) / [Wren](#in-Wren) / [GNU Smalltalk](#in-GNU-Smalltalk) / [JScript](#in-JScript) / [Pharo](#in-Pharo) / [Scratch](#in-Scratch) / [Standard ML](#in-Standard-ML) / [なでしこ](#in-なでしこ) / [Kuin](#in-Kuin) / [ClojureScript](#in-ClojureScript) / [HSP](#in-HSP) / [Reason](#in-Reason) / [THPL](#in-THPL) / [Janet](#in-Janet) / [Phel](#in-Phel) / [Raku](#in-Raku) / [文言](#in-文言) / [Vala](#in-Vala) / [SmileBASIC](#in-SmileBASIC) / [Small Basic](#in-Small-Basic) / [Flix](#in-Flix) / [PowerShell](#in-PowerShell) / [Koka](#in-Koka) / [Zig](#in-Zig) / [BanchaScript](#in-BanchaScript) / [AssemblyScript](#in-AssemblyScript) / [Clawn](#in-Clawn) / [FORTRAN 77](#in-FORTRAN-77) / [orelang](#in-orelang) / [Laze](#in-Laze) / [WebAssembly](#in-WebAssembly) / [Nelua](#in-Nelua) / [Roland](#in-Roland) / [ReScript](#in-ReScript) / [Effekt](#in-Effekt) / [Ceylon](#in-Ceylon) / [Mochi](#in-Mochi) / [Neko](#in-Neko) / [NekoML](#in-NekoML) / [LLVM IR](#in-LLVM-IR) / [Mojo](#in-Mojo) / [Pkl](#in-Pkl) / [Jsonnet](#in-Jsonnet) / [AppleScript](#in-AppleScript) / [DNCL3](#in-DNCL3) / [Wirth](#in-Wirth) / [C--](#in-C-3))
1111

1212
## What is Geo3x3
1313
Geo3x3 is a simple geo-coding system for WGS84
@@ -2440,6 +2440,59 @@ to run:
24402440
$ deno -A https://code4fukui.github.io/Wirth/cli.js geo3x3.wirth
24412441
```
24422442

2443+
## in C--
2444+
[C--](https://github.com/tctsigemura/C--)
2445+
[geo3x3.hmm](https://github.com/taisukef/Geo3x3/blob/master/geo3x3.hmm)
2446+
```
2447+
#include <stdio.hmm>
2448+
#include "geo3x3.hmm"
2449+
2450+
int[] lat = {0, 3, 5, 6, 5, 8, 5, 8, 0};
2451+
int[] lng = {1, 3, 9, 7, 4, 5, 4, 3, 3};
2452+
2453+
public int main() {
2454+
char[] code = geo3x3_encode(lat, lng, 14);
2455+
if (code == null) {
2456+
printf("err geo3x3_encode\n");
2457+
return 1;
2458+
}
2459+
printf("geo3x3_encode: %s\n", code); // geo3x3: E9139519253557
2460+
free(code);
2461+
2462+
int n = 9;
2463+
void[][] res = geo3x3_decode("E9139659937288", n);
2464+
printf("geo3x3_decode:\n");
2465+
printf(" lat: ");
2466+
show(res[0], n);
2467+
printf(" lng: ");
2468+
show(res[1], n);
2469+
int[] level2 = res[2];
2470+
printf(" level: %d\n", level2[0]);
2471+
printf(" unit: ");
2472+
show(res[3], n);
2473+
for (int i = 0; i < 4; i = i + 1) {
2474+
free(res[i]);
2475+
}
2476+
free(res);
2477+
return 0;
2478+
}
2479+
```
2480+
setup:
2481+
```
2482+
clone https://github.com/tctsigemura/C--.git
2483+
cd C--/src
2484+
make
2485+
sudo make install
2486+
cd ../lib
2487+
sudo make install
2488+
cd ../include
2489+
sudo make install
2490+
```
2491+
to run:
2492+
```bash
2493+
$ cm2c simple_geo3x3.cmm; ./a.out
2494+
```
2495+
24432496
## How to contribute
24442497
1. add a lang
24452498
2. add a lang section in [README.md](README.md) (ex. [Neko](#in-Neko))

geo3x3.hmm

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
#include <stdlib.hmm>
2+
3+
int[] create(int val, int n) {
4+
int[] res = iMalloc(n);
5+
for (int i = 0; i < n; i = i + 1) {
6+
res[i] = 0;
7+
}
8+
res[0] = val / 100;
9+
res[1] = val / 10 % 10;
10+
res[2] = val % 10;
11+
return res;
12+
}
13+
14+
void show(int[] val, int n) {
15+
for (int i = 0; i < n; i = i + 1) {
16+
printf("%d ", val[i]);
17+
}
18+
printf("\n");
19+
}
20+
21+
void norm(int[] a, int n) {
22+
for (int i = n - 1; i >= 1; i = i - 1) {
23+
a[i - 1] = a[i - 1] + a[i] / 10;
24+
a[i] = a[i] % 10;
25+
}
26+
}
27+
28+
void add(int[] a, int[] b, int[] c, int n) {
29+
for (int i = 0; i < n; i = i + 1) {
30+
c[i] = a[i] + b[i];
31+
}
32+
norm(c, n);
33+
}
34+
35+
void sub(int[] a, int[] b, int[] c, int n) {
36+
for (int i = n - 1; i >= 0; i = i - 1) {
37+
if (a[i] < b[i] && i > 0) {
38+
a[i - 1] = a[i - 1] - 1;
39+
a[i] = a[i] + 10;
40+
}
41+
c[i] = a[i] - b[i];
42+
}
43+
}
44+
45+
void xmul(int[] a, int x, int n) {
46+
for (int i = n - 1; i >= 0; i = i - 1) {
47+
a[i] = a[i] * x;
48+
}
49+
norm(a, n);
50+
}
51+
52+
void xdiv(int[] a, int x, int n) {
53+
for (int i = 0; i < n; i = i + 1) {
54+
int d = a[i] % x;
55+
a[i] = a[i] / x;
56+
if (i < n - 1) {
57+
a[i + 1] = a[i + 1] + d * 10;
58+
}
59+
}
60+
}
61+
62+
void a2b(int[] a, int[] b, int n) {
63+
for (int i = 0; i < n; i = i + 1) {
64+
b[i] = a[i];
65+
}
66+
}
67+
68+
int divx(int[] a, int[] b, int n) {
69+
int[] a2 = create(0, n);
70+
int[]c = create(0, n);
71+
for (int i = 0; i < n; i = i + 1) {
72+
a2[i] = a[i];
73+
c[i] = 0;
74+
}
75+
int x = 0;
76+
for (;;) {
77+
sub(a2, b, c, n);
78+
if (c[0] < 0) {
79+
break;
80+
}
81+
x = x + 1;
82+
a2b(c, a2, n);
83+
}
84+
free(a2);
85+
free(c);
86+
return x;
87+
}
88+
89+
public char[] geo3x3_encode(int[] lat, int[] lng, int level) {
90+
int n = 9;
91+
char[] code = cMalloc(level + 1);
92+
lat[1] = lat[1] + 9;
93+
norm(lat, n);
94+
if (lng[0] >= 0) {
95+
code[0] = 'E';
96+
} else {
97+
code[0] = 'W';
98+
int[] n180 = create(180, n);
99+
add(lng, n180, lng, n);
100+
free(n180);
101+
}
102+
int[] temp = create(0, n);
103+
int[] unit = create(180, n);
104+
for (int i = 1; i < level; i = i + 1) {
105+
xdiv(unit, 3, n);
106+
int y = divx(lat, unit, n);
107+
int x = divx(lng, unit, n);
108+
code[i] = chr('0' + (x + y * 3 + 1));
109+
a2b(unit, temp, n);
110+
xmul(temp, x, n);
111+
sub(lng, temp, lng, n);
112+
a2b(unit, temp, n);
113+
xmul(temp, y, n);
114+
sub(lat, temp, lat, n);
115+
}
116+
code[level] = '\0';
117+
free(temp);
118+
free(unit);
119+
return code;
120+
}
121+
122+
public int[][] geo3x3_decode(char[] code, int n) {
123+
boolean flg = false;
124+
if (code[0] == 'W') {
125+
flg = true;
126+
}
127+
int[] unit = create(180, n);
128+
int[] lat = create(0, n);
129+
int[] lng = create(0, n);
130+
int[] temp = create(0, n);
131+
int level = 1;
132+
for (int i = 1;; i = i + 1) {
133+
char c = code[i];
134+
if (c == '\0') {
135+
break;
136+
}
137+
int m = c - '1';
138+
xdiv(unit, 3, n);
139+
a2b(unit, temp, n);
140+
xmul(temp, m % 3, n);
141+
add(lng, temp, lng, n);
142+
a2b(unit, temp, n);
143+
xmul(temp, m / 3, n);
144+
add(lat, temp, lat, n);
145+
level = level + 1;
146+
}
147+
a2b(unit, temp, n);
148+
xdiv(temp, 2, n);
149+
add(lat, temp, lat, n);
150+
add(lng, temp, lng, n);
151+
int[] n90 = create(90, n);
152+
sub(lat, n90, lat, n);
153+
free(n90);
154+
if (flg) {
155+
int[] n180 = create(180, n);
156+
sub(lng, n180, lng, n);
157+
free(n180);
158+
}
159+
int[] level2 = iMalloc(1);
160+
level2[0] = level;
161+
void[][] res = rMalloc(4);
162+
res[0] = lat;
163+
res[1] = lng;
164+
res[2] = level2;
165+
res[3] = unit;
166+
return res;
167+
}

langlist.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,4 @@ Jsonnet,"jsonnet,libsonnet",none,none,local,script,https://github.com/taisukef/G
108108
AppleScript,applescript,end,none,none,script,https://github.com/taisukef/Geo3x3/blob/master/geo3x3_encode.applescript
109109
DNCL3,dncl,blacket,none,none,script,https://github.com/taisukef/Geo3x3/blob/master/geo3x3.dncl
110110
Wirth,wirth,end,none,none,script,https://github.com/taisukef/Geo3x3/blob/master/geo3x3.wirth
111+
C--,"cmm,hmm",blacket,int,none,compile,https://github.com/taisukef/Geo3x3/blob/master/geo3x3.hmm

simple_geo3x3.c

Lines changed: 0 additions & 40 deletions
This file was deleted.

simple_geo3x3.cmm

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <stdio.hmm>
2+
#include <stdlib.hmm>
3+
#include "geo3x3.hmm"
4+
5+
int[] lat = {0, 3, 5, 6, 5, 8, 5, 8, 0};
6+
int[] lng = {1, 3, 9, 7, 4, 5, 4, 3, 3};
7+
8+
public int main() {
9+
char[] code = geo3x3_encode(lat, lng, 14);
10+
if (code == null) {
11+
printf("err geo3x3_encode\n");
12+
return 1;
13+
}
14+
printf("geo3x3_encode: %s\n", code); // geo3x3: E9139519253557
15+
free(code);
16+
17+
int n = 9;
18+
void[][] res = geo3x3_decode("E9139659937288", n);
19+
printf("geo3x3_decode:\n");
20+
printf(" lat: ");
21+
show(res[0], n);
22+
printf(" lng: ");
23+
show(res[1], n);
24+
int[] level2 = res[2];
25+
printf(" level: %d\n", level2[0]);
26+
printf(" unit: ");
27+
show(res[3], n);
28+
for (int i = 0; i < 4; i = i + 1) {
29+
free(res[i]);
30+
}
31+
free(res);
32+
return 0;
33+
}

0 commit comments

Comments
 (0)