Skip to content
Closed
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
11 changes: 11 additions & 0 deletions lib/linguist/languages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1301,6 +1301,17 @@ Clue:
tm_scope: source.clue
ace_mode: text
language_id: 163763508
Cobrust:
type: programming
color: "#b45309"
extensions:
- ".cb"
tm_scope: source.cobrust
ace_mode: python
codemirror_mode: python
codemirror_mime_type: text/x-python
aliases:
- cobrust
CoNLL-U:
type: data
extensions:
Expand Down
14 changes: 14 additions & 0 deletions samples/Cobrust/fib.cb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Fibonacci — M11.2 example (ADR-0034).
#
# Real recursive algorithm. Demonstrates user-defined fn calls via
# `Constant::FnRef` Call lowering (forward-declaration two-pass per
# ADR-0034 §"Decision" Option 3). fib(10) = 55.
fn fib(n: i64) -> i64:
if n < 2:
return n
return fib(n - 1) + fib(n - 2)

fn main() -> i64:
print("fib(10) =")
print(fib(10))
return 0
24 changes: 24 additions & 0 deletions samples/Cobrust/fizzbuzz.cb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# FizzBuzz — M11.1 example (ADR-0030).
#
# Real algorithm: while loop + if/elif/elif/else + modulo arithmetic.
# Demonstrates:
# - while loop with mutation
# - if/elif/elif/else with if as the first statement in the loop body
# - modulo operator (%)
# - print (polymorphic: string and integer, ADR-0064)
#
# The M11 prelude fixes (ADR-0030) enable this after the while-loop-
# with-leading-if codegen regression was resolved.
fn main() -> i64:
let n: i64 = 1
while n <= 15:
if n % 15 == 0:
print("FizzBuzz")
elif n % 3 == 0:
print("Fizz")
elif n % 5 == 0:
print("Buzz")
else:
print(n)
n = n + 1
return 0
3 changes: 3 additions & 0 deletions samples/Cobrust/hello.cb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() -> i64:
print("hello, world")
return 0
31 changes: 31 additions & 0 deletions samples/Cobrust/two_sum.cb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# LC-01 Two Sum (ADR-0044 W2 Phase 3).
#
# Input (stdin):
# Line 1: N
# Lines 2..N+1: one integer each
# Line N+2: target
#
# Output: indices i j (i < j) with nums[i]+nums[j]==target, one per line.
#
# Algorithm: O(N²) brute-force scan.

fn main() -> i64:
let n = parse_int(input(""))
let nums = list_new(n)
let i: i64 = 0
while i < n:
let v = parse_int(input(""))
list_set(nums, i, v)
i = i + 1
let target = parse_int(input(""))
let a: i64 = 0
while a < n:
let b: i64 = a + 1
while b < n:
if list_get(nums, a) + list_get(nums, b) == target:
print(a)
print(b)
return 0
b = b + 1
a = a + 1
return 0
37 changes: 37 additions & 0 deletions samples/Cobrust/valid_anagram.cb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# LC-022 hashmap-valid-anagram (ADR-0047 LC-100 Tier A bucket B1).
#
# Input (stdin): Line 1: string s; Line 2: string t
# Output: "true" if s and t are anagrams, else "false"
# Algorithm: 26-slot frequency count; increment for s, decrement for t

fn main() -> i64:
let s = input("")
let t = input("")
let sn = str_len(&s)
let tn = str_len(&t)
if sn != tn:
print("false")
return 0
let freq = list_new(26)
let i: i64 = 0
while i < 26:
list_set(freq, i, 0)
i = i + 1
let j: i64 = 0
while j < sn:
let code = str_ord(str_at(&s, j)) - 97
list_set(freq, code, list_get(freq, code) + 1)
j = j + 1
let k: i64 = 0
while k < tn:
let code2 = str_ord(str_at(&t, k)) - 97
list_set(freq, code2, list_get(freq, code2) - 1)
k = k + 1
let m: i64 = 0
while m < 26:
if list_get(freq, m) != 0:
print("false")
return 0
m = m + 1
print("true")
return 0