docs/getting-started.md
Anchor is a statically-typed programming language that transpiles to C99.
Anchor transpiles to C99 and needs a C compiler to produce binaries. One of the following must be installed and available in your PATH:
Build the compiler from source:
cmake -B build
cmake --build build
This produces the bin/ancc.exe compiler binary.
Create a file called hello.anc:
func main(): int
return 0
end
Compile and run it:
bin/ancc.exe run hello.anc
struct Point
x: int
y: int
func manhattan(): int
var ax = self.x
if ax < 0
ax = -ax
end
var ay = self.y
if ay < 0
ay = -ay
end
return ax + ay
end
end
func main(): int
var p = Point(x = 3, y = -4)
return p.manhattan()
end
For projects with multiple files, create a package. A package needs a manifest file named anchor (no extension) in the project root:
name myproject
entry main
Organize source files under src/:
myproject/
anchor
src/
main.anc
utils.anc
core/
math.anc
Import symbols between modules using dot-separated paths that map to directories:
# In src/main.anc
from utils import sign
from core.math import abs
func main(): int
return abs(sign(-5))
end
# In src/utils.anc
export func sign(x: int): int
if x < 0
return -1
elseif x > 0
return 1
else
return 0
end
end
# In src/core/math.anc
export func abs(x: int): int
if x < 0
return -x
else
return x
end
end
Build the project:
bin/ancc.exe build myproject
Only symbols marked with export are visible to other modules.