New Language: C3 (#5086)

Related to: https://github.com/compiler-explorer/infra/pull/1017

C3 is a system programming language based on C. It is an evolution of C
enabling the same paradigms and retaining the same syntax as far as
possible.

Design Principles:
Procedural "get things done"-type of language.
Try to stay close to C - only change what's really necessary.
C ABI compatibility and excellent C integration.
Learning C3 should be easy for a C programmer.
Data is inert.
Avoid "big ideas" & the "more is better" fallacy.
Introduce some higher level conveniences where the value is great.

You can try it out live on its tutorial website:
https://www.learn-c3.org
Source code: https://github.com/c3lang/c3c

---------

Co-authored-by: Rubén Rincón Blanco <git@rinconblanco.es>
This commit is contained in:
aliaegik
2023-06-02 16:38:57 +03:30
committed by GitHub
parent 46f142f4d8
commit 91334b9d9b
12 changed files with 412 additions and 0 deletions

4
.github/labeler.yml vendored
View File

@@ -29,6 +29,10 @@ lang-c++:
lang-c++-opencl:
- etc/config/cpp_for_opencl.*.properties
- static/modes/cpp-for-opencl-mode.ts
lang-c3:
- lib/compilers/c3.ts
- etc/config/c3.*.properties
- static/modes/c3-mode.ts
lang-circle:
- lib/compilers/circle.js
- etc/config/circle.*.properties

View File

@@ -129,3 +129,4 @@ From oldest to newest contributor, we would like to thank:
- [Madhur Chauhan](https://github.com/madhur4127)
- [VoltrexKeyva](https://github.com/VoltrexKeyva)
- [Johel Ernesto Guerrero Peña](https://github.com/JohelEGP)
- [Ali](https://github.com/aliaegik)

View File

@@ -0,0 +1,11 @@
compilers=&c3c
compilerType=c3c
defaultCompiler=c3c04
supportsBinary=false
supportsBinaryObject=false
supportsExecute=false
group.c3c.compilers=c3c04
group.c3c.isSemVer=true
group.c3c.baseName=c3
compiler.c3c04.semver=0.4
compiler.c3c04.exe=/opt/compiler-explorer/c3-0.4/c3c

View File

@@ -0,0 +1,8 @@
compilers=c3cdefault
compilerType=c3c
defaultCompiler=c3cdefault
supportsBinary=false
supportsBinaryObject=false
supportsExecute=false
compiler.c3cdefault.name=c3c
compiler.c3cdefault.exe=/usr/bin/c3c/build/c3c

8
examples/c3/default.c3 Normal file
View File

@@ -0,0 +1,8 @@
module output; // necessary for compiler explorer
import std::io;
fn void main()
{
io::printfn("hello, world");
}

View File

@@ -27,6 +27,7 @@ export {AnalysisTool} from './analysis-tool.js';
export {AssemblyCompiler} from './assembly.js';
export {AvrGcc6502Compiler} from './avrgcc6502.js';
export {BeebAsmCompiler} from './beebasm.js';
export {C3Compiler} from './c3c.js';
export {CarbonCompiler} from './carbon.js';
export {Cc65Compiler} from './cc65.js';
export {CircleCompiler} from './circle.js';

24
lib/compilers/c3c.ts Normal file
View File

@@ -0,0 +1,24 @@
import path from 'path';
import type {PreliminaryCompilerInfo} from '../../types/compiler.interfaces.js';
import type {ParseFiltersAndOutputOptions} from '../../types/features/filters.interfaces.js';
import {BaseCompiler} from '../base-compiler.js';
export class C3Compiler extends BaseCompiler {
static get key() {
return 'c3c';
}
constructor(info: PreliminaryCompilerInfo, env) {
super(info, env);
this.compiler.supportsIrView = true;
this.compiler.irArg = ['--emit-llvm'];
}
override optionsForFilter(filters: ParseFiltersAndOutputOptions, outputFilename: string) {
return ['compile-only', '-g', '-l', 'pthread', '--emit-asm'];
}
override getIrOutputFilename(inputFilename: string, filters: ParseFiltersAndOutputOptions): string {
return this.filename(path.dirname(inputFilename) + '/output.ll');
}
}

View File

@@ -110,6 +110,17 @@ const definitions: Record<LanguageKey, LanguageDefinition> = {
previewFilter: /^\s*#include/,
monacoDisassembly: null,
},
c3: {
name: 'C3',
monaco: 'c3',
extensions: ['.c3'],
alias: [],
logoUrl: 'c3.svg',
logoUrlDark: null,
formatter: null,
previewFilter: null,
monacoDisassembly: null,
},
carbon: {
name: 'Carbon',
monaco: 'carbon',

View File

@@ -26,6 +26,7 @@ import './ada-mode';
import './asm6502-mode';
import './asm-mode';
import './asmruby-mode';
import './c3-mode';
import './carbon-mode';
import './clean-mode';
import './cmake-mode';

341
static/modes/c3-mode.ts Normal file
View File

@@ -0,0 +1,341 @@
import * as monaco from 'monaco-editor';
function definition(): monaco.languages.IMonarchLanguage {
return {
defaultToken: 'invalid',
keywords: [
'alias',
'as',
'assert',
'asm',
'bitstruct',
'break',
'case',
'catch',
'const',
'continue',
'define',
'default',
'defer',
'do',
'else',
'enum',
'extern',
'false',
'fault',
'for',
'foreach',
'foreach_r',
'fn',
'generic',
'tlocal',
'if',
'import',
'macro',
'module',
'nextcase',
'null',
'private',
'return',
'static',
'struct',
'switch',
'true',
'try',
'union',
'var',
'while',
'typedef',
'distinct',
'initialize',
'finalize',
'inline',
'$alignof',
'$assert',
'$case',
'$checks',
'$default',
'$defined',
'$echo',
'$elif',
'$else',
'$endfor',
'$endforeach',
'$endif',
'$endswitch',
'$eval',
'$evaltype',
'$extnameof',
'$for',
'$foreach',
'$if',
'$include',
'$nameof',
'$offsetof',
'$qnameof',
'$sizeof',
'$stringify',
'$switch',
'$vacount',
'$vaconst',
'$varef',
'$vaarg',
'$vaexpr',
'$vasplat',
'$$abs',
'$$bitreverse',
'$$bswap',
'$$ceil',
'$$compare_exchange',
'$$copysign',
'$$cos',
'$$clz',
'$$ctz',
'$$add',
'$$div',
'$$mod',
'$$mul',
'$$neg',
'$$sub',
'$$exp',
'$$exp2',
'$$expect',
'$$expect_with_probability',
'$$floor',
'$$fma',
'$$fmuladd',
'$$frameaddress',
'$$fshl',
'$$fshr',
'$$get_rounding_mode',
'$$log',
'$$log10',
'$$log2',
'$$max',
'$$memcpy',
'$$memcpy_inline',
'$$memmove',
'$$memset',
'$$memset_inline',
'$$min',
'$$nearbyint',
'$$overflow_add',
'$$overflow_mul',
'$$overflow_sub',
'$$popcount',
'$$pow',
'$$pow_int',
'$$prefetch',
'$$reduce_add',
'$$reduce_and',
'$$reduce_fadd',
'$$reduce_fmul',
'$$reduce_max',
'$$reduce_min',
'$$reduce_mul',
'$$reduce_or',
'$$reduce_xor',
'$$reverse',
'$$rint',
'$$round',
'$$roundeven',
'$$sat_add',
'$$sat_shl',
'$$sat_sub',
'$$set_rounding_mode',
'$$swizzle',
'$$swizzle2',
'$$sin',
'$$sqrt',
'$$stacktrace',
'$$syscall',
'$$sysclock',
'$$trap',
'$$trunc',
'$$unreachable',
'$$veccomplt',
'$$veccomple',
'$$veccompgt',
'$$veccompge',
'$$veccompeq',
'$$veccompne',
'$$volatile_load',
'$$volatile_store',
'$$wasm_memory_size',
'$$wasm_memory_grow',
'$$DATE',
'$$FILE',
'$$FILEPATH',
'$$FUNC',
'$$FUNCTION',
'$$LINE',
'$$LINE_RAW',
'$$MODULE',
'$$TEST_NAMES',
'$$TEST_FNS',
'$$TIME',
],
typeKeywords: [
'anyfault',
'any',
'void',
'bool',
'char',
'double',
'float16',
'bfloat16',
'float128',
'int128',
'int',
'ichar',
'iptr',
'isz',
'long',
'short',
'uint128',
'uint',
'ulong',
'uptr',
'ushort',
'usz',
'float',
'typeid',
'ireg',
'ureg',
'$vatype',
'$typeof',
'$typefrom',
],
operators: [
'+',
'+%',
'-',
'-%',
'/',
'*',
'*%',
'=',
'^',
'&',
'?',
'|',
'!',
'>',
'<',
'%',
'<<',
'<<%',
'>>',
'+=',
'+%=',
'-=',
'-%=',
'/=',
'*=',
'*%=',
'==',
'^=',
'&=',
'?=',
'|=',
'!=',
'>=',
'<=',
'%=',
'<<=',
'<<%=',
'>>=',
],
symbols: /[=><!~?:&|+\-*/^%]+/,
escapes: /\\(?:[abfnrtv\\"']|x[0-9A-Fa-f]{1,4}|u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8})/,
tokenizer: {
root: [
// u0/i0 integer types
[/[iu]\d+/, 'keyword'],
// identifiers and keywords
[
/[a-z_$][\w$]*/,
{
cases: {
'@typeKeywords': 'keyword',
'@keywords': 'keyword',
'@default': 'identifier',
},
},
],
[/@[a-zA-Z_$]*/, 'builtin.identifier'],
[/[A-Z][\w$]*/, 'type.identifier'], // to show class names nicely
// whitespace
{include: '@whitespace'},
// delimiters and operators
[/[{}()[\]]/, '@brackets'],
[/[<>](?!@symbols)/, '@brackets'],
[
/@symbols/,
{
cases: {
'@operators': 'operator',
'@default': '',
},
},
],
// numbers
[/\d*\.\d+([eE][-+]?\d+)?[fFdD]?/, 'number.float'],
[/0[xX][0-9a-fA-F_]*[0-9a-fA-F][Ll]?/, 'number.hex'],
[/0o[0-7_]*[0-7][Ll]?/, 'number.octal'],
[/0[bB][0-1_]*[0-1][Ll]?/, 'number.binary'],
[/\d+/, 'number'],
// delimiter: after number because of .\d floats
[/[;,.]/, 'delimiter'],
// strings
[/"([^"\\]|\\.)*$/, 'string.invalid'], // non-teminated string
[/c?\\\\.*$/, 'string'],
[/c?"/, 'string', '@string'],
// characters
[/'[^\\']'/, 'string'],
[/(')(@escapes)(')/, ['string', 'string.escape', 'string']],
[/'/, 'string.invalid'],
],
whitespace: [
[/[ \r\n]+/, 'white'],
[/\/\*/, 'comment', '@comment'],
[/\/\+/, 'comment', '@comment'],
[/\/\/.*$/, 'comment'],
[/\t/, 'comment.invalid'],
],
comment: [
[/[^/*]+/, 'comment'],
[/\/\*/, 'comment', '@comment'],
[/\*\//, 'comment', '@pop'],
[/[/*]/, 'comment'],
],
string: [
[/[^\\"]+/, 'string'],
[/@escapes/, 'string.escape'],
[/\\./, 'string.escape.invalid'],
[/"/, 'string', '@pop'],
],
},
};
}
monaco.languages.register({id: 'c3'});
monaco.languages.setMonarchTokensProvider('c3', definition());
export {};

View File

@@ -28,6 +28,7 @@ export type LanguageKey =
| 'assembly'
| 'c'
| 'c++'
| 'c3'
| 'carbon'
| 'circle'
| 'circt'

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 5.1 KiB