diff --git a/lib/linguist/languages.yml b/lib/linguist/languages.yml index dbde1fd01d..172868aed6 100644 --- a/lib/linguist/languages.yml +++ b/lib/linguist/languages.yml @@ -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: diff --git a/samples/Cobrust/fib.cb b/samples/Cobrust/fib.cb new file mode 100644 index 0000000000..7875328e69 --- /dev/null +++ b/samples/Cobrust/fib.cb @@ -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 diff --git a/samples/Cobrust/fizzbuzz.cb b/samples/Cobrust/fizzbuzz.cb new file mode 100644 index 0000000000..b2bfe1c2d2 --- /dev/null +++ b/samples/Cobrust/fizzbuzz.cb @@ -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 diff --git a/samples/Cobrust/hello.cb b/samples/Cobrust/hello.cb new file mode 100644 index 0000000000..6dec94a81e --- /dev/null +++ b/samples/Cobrust/hello.cb @@ -0,0 +1,3 @@ +fn main() -> i64: + print("hello, world") + return 0 diff --git a/samples/Cobrust/two_sum.cb b/samples/Cobrust/two_sum.cb new file mode 100644 index 0000000000..986ea17214 --- /dev/null +++ b/samples/Cobrust/two_sum.cb @@ -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 diff --git a/samples/Cobrust/valid_anagram.cb b/samples/Cobrust/valid_anagram.cb new file mode 100644 index 0000000000..d059900a73 --- /dev/null +++ b/samples/Cobrust/valid_anagram.cb @@ -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