Skip to content

Commit 4e7368e

Browse files
committed
add support for C3, #961
1 parent 7bc6bdb commit 4e7368e

5 files changed

Lines changed: 215 additions & 6 deletions

File tree

Unix/cloc

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8925,6 +8925,9 @@ sub set_constants { # {{{1
89258925
'ixx' => 'C++' ,
89268926
'tcc' => 'C++' ,
89278927
'tpp' => 'C++' ,
8928+
'cxx' => 'C++' ,
8929+
'cpp' => 'C++' ,
8930+
'CPP' => 'C++' ,
89288931
'cdc' => 'Cadence' ,
89298932
'ccs' => 'CCS' ,
89308933
'civet' => 'Civet' ,
@@ -8966,8 +8969,6 @@ sub set_constants { # {{{1
89668969
'cg3' => 'Constraint Grammar' ,
89678970
'rlx' => 'Constraint Grammar' ,
89688971
'Containerfile' => 'Containerfile' ,
8969-
'cpp' => 'C++' ,
8970-
'CPP' => 'C++' ,
89718972
'cr' => 'Crystal' ,
89728973
'cs' => 'C#/Smalltalk' ,
89738974
'designer.cs' => 'C# Designer' ,
@@ -8978,7 +8979,9 @@ sub set_constants { # {{{1
89788979
'csv' => "CSV" ,
89798980
'cu' => 'CUDA' ,
89808981
'cuh' => 'CUDA' , # CUDA header file
8981-
'cxx' => 'C++' ,
8982+
'c3' => 'C3' ,
8983+
'c3i' => 'C3' ,
8984+
'c3t' => 'C3' ,
89828985
'd' => 'D/dtrace' ,
89838986
# in addition, .d can map to init.d files typically written as
89848987
# bash or sh scripts
@@ -10250,6 +10253,12 @@ sub set_constants { # {{{1
1025010253
'Cucumber' => [
1025110254
[ 'remove_matches' , '^\s*#' ],
1025210255
],
10256+
'C3' => [
10257+
[ 'remove_matches' , '^\s*//' ],
10258+
[ 'remove_between_general', '<*', '*>' ],
10259+
[ 'call_regexp_common' , 'C++' ],
10260+
[ 'remove_inline' , '//.*$' ],
10261+
],
1025310262
'D/dtrace' => [ [ 'die' , ], ], # never called
1025410263
'D' => [
1025510264
[ 'rm_comments_in_strings', '"', '/*', '*/' ],
@@ -12057,6 +12066,7 @@ sub set_constants { # {{{1
1205712066
'CSV' => 0.1,
1205812067
'Cucumber' => 3.00,
1205912068
'CUDA' => 1.00,
12069+
'C3' => 1.61,
1206012070
'D' => 1.70,
1206112071
'Dafny' => 3.00,
1206212072
'DAL' => 1.50,

Unix/t/00_C.t

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,11 @@ my @Tests = (
304304
'ref' => '../tests/outputs/cucumber.feature.yaml',
305305
'args' => '../tests/inputs/cucumber.feature',
306306
},
307+
{
308+
'name' => 'C3',
309+
'ref' => '../tests/outputs/io.c3.yaml',
310+
'args' => '../tests/inputs/io.c3',
311+
},
307312
{
308313
'name' => 'Dafny',
309314
'ref' => '../tests/outputs/fib.dfy.yaml',

cloc

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8917,6 +8917,9 @@ sub set_constants { # {{{1
89178917
'ixx' => 'C++' ,
89188918
'tcc' => 'C++' ,
89198919
'tpp' => 'C++' ,
8920+
'cxx' => 'C++' ,
8921+
'cpp' => 'C++' ,
8922+
'CPP' => 'C++' ,
89208923
'cdc' => 'Cadence' ,
89218924
'ccs' => 'CCS' ,
89228925
'civet' => 'Civet' ,
@@ -8958,8 +8961,6 @@ sub set_constants { # {{{1
89588961
'cg3' => 'Constraint Grammar' ,
89598962
'rlx' => 'Constraint Grammar' ,
89608963
'Containerfile' => 'Containerfile' ,
8961-
'cpp' => 'C++' ,
8962-
'CPP' => 'C++' ,
89638964
'cr' => 'Crystal' ,
89648965
'cs' => 'C#/Smalltalk' ,
89658966
'designer.cs' => 'C# Designer' ,
@@ -8970,7 +8971,9 @@ sub set_constants { # {{{1
89708971
'csv' => "CSV" ,
89718972
'cu' => 'CUDA' ,
89728973
'cuh' => 'CUDA' , # CUDA header file
8973-
'cxx' => 'C++' ,
8974+
'c3' => 'C3' ,
8975+
'c3i' => 'C3' ,
8976+
'c3t' => 'C3' ,
89748977
'd' => 'D/dtrace' ,
89758978
# in addition, .d can map to init.d files typically written as
89768979
# bash or sh scripts
@@ -10242,6 +10245,12 @@ sub set_constants { # {{{1
1024210245
'Cucumber' => [
1024310246
[ 'remove_matches' , '^\s*#' ],
1024410247
],
10248+
'C3' => [
10249+
[ 'remove_matches' , '^\s*//' ],
10250+
[ 'remove_between_general', '<*', '*>' ],
10251+
[ 'call_regexp_common' , 'C++' ],
10252+
[ 'remove_inline' , '//.*$' ],
10253+
],
1024510254
'D/dtrace' => [ [ 'die' , ], ], # never called
1024610255
'D' => [
1024710256
[ 'rm_comments_in_strings', '"', '/*', '*/' ],
@@ -12049,6 +12058,7 @@ sub set_constants { # {{{1
1204912058
'CSV' => 0.1,
1205012059
'Cucumber' => 3.00,
1205112060
'CUDA' => 1.00,
12061+
'C3' => 1.61,
1205212062
'D' => 1.70,
1205312063
'Dafny' => 3.00,
1205412064
'DAL' => 1.50,

tests/inputs/io.c3

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
// https://github.com/c3lang/c3c/blob/master/lib/std/io/io.c3 (partial saved here)
2+
// Copyright (c) 2021-2025 Christoffer Lerno. All rights reserved.
3+
// Use of this source code is governed by the MIT license
4+
// a copy of which can be found in the LICENSE_STDLIB file.
5+
module std::io;
6+
import libc;
7+
8+
enum Seek
9+
{
10+
SET,
11+
CURSOR,
12+
END
13+
}
14+
15+
faultdef
16+
ALREADY_EXISTS,
17+
BUSY,
18+
CANNOT_READ_DIR,
19+
DIR_NOT_EMPTY,
20+
PARENT_DIR_MISSING,
21+
EOF,
22+
FILE_CANNOT_DELETE,
23+
FILE_IS_DIR,
24+
FILE_IS_PIPE,
25+
FILE_NOT_DIR,
26+
FILE_NOT_FOUND,
27+
FILE_NOT_VALID,
28+
GENERAL_ERROR,
29+
ILLEGAL_ARGUMENT,
30+
INCOMPLETE_WRITE,
31+
INTERRUPTED,
32+
INVALID_POSITION,
33+
INVALID_PUSHBACK,
34+
NAME_TOO_LONG,
35+
NOT_SEEKABLE,
36+
NO_PERMISSION,
37+
OUT_OF_SPACE,
38+
OVERFLOW,
39+
PATH_COULD_NOT_BE_FOUND,
40+
READ_ONLY,
41+
SYMLINK_FAILED,
42+
TOO_MANY_DESCRIPTORS,
43+
UNEXPECTED_EOF,
44+
UNKNOWN_ERROR,
45+
UNSUPPORTED_OPERATION,
46+
WOULD_BLOCK;
47+
48+
49+
<*
50+
Read from a stream (default is stdin) to the next "\n"
51+
or to the end of the stream, whatever comes first.
52+
"\r" will be filtered from the String.
53+
54+
@param [&inout] allocator : "The allocator used to allocate the read string."
55+
@param stream : `The stream to read from.`
56+
@param limit : `Optionally limits the amount of bytes to read in a single line. Will NOT discard the remaining line data.`
57+
@require @is_not_instream_if_ptr(stream) : "The value for 'stream' should have been passed as a pointer and not as a value, please add '&'."
58+
@require @is_instream(stream) : `Make sure that the stream is actually an InStream.`
59+
@param [inout] allocator : `the allocator to use.`
60+
@return `The string containing the data read.`
61+
*>
62+
macro String? readline(Allocator allocator, stream = io::stdin(), usz limit = 0)
63+
{
64+
return readline_impl{$typeof(stream)}(allocator, stream, limit);
65+
}
66+
67+
<*
68+
Reads a string, see `readline`, except the it is allocated
69+
on the temporary allocator and does not need to be freed.
70+
71+
@param stream : `The stream to read from.`
72+
@param limit : `Optionally limits the amount of bytes to read in a single line. Will NOT discard the remaining line data.`
73+
@require @is_not_instream_if_ptr(stream) : "The value for 'stream' should have been passed as a pointer and not as a value, please add '&'."
74+
@require @is_instream(stream) : `The stream must implement InStream.`
75+
@return `The temporary string containing the data read.`
76+
*>
77+
macro String? treadline(stream = io::stdin(), usz limit = 0)
78+
{
79+
return readline(tmem, stream, limit) @inline;
80+
}
81+
82+
fn String? readline_impl(Allocator allocator, Stream stream, usz limit) <Stream> @private
83+
{
84+
if (allocator == tmem)
85+
{
86+
DString str = dstring::temp_with_capacity(256);
87+
readline_to_stream(&str, stream, limit)!;
88+
return str.str_view();
89+
}
90+
@pool()
91+
{
92+
DString str = dstring::temp_with_capacity(256);
93+
readline_to_stream(&str, stream, limit)!;
94+
return str.copy_str(allocator);
95+
};
96+
}
97+
98+
<*
99+
Reads a string, see `readline`, the data is passed to an outstream
100+
101+
@param out_stream : `The stream to write to`
102+
@param in_stream : `The stream to read from.`
103+
@param limit : `Optionally limits the byte-length of the allocated output string.`
104+
@require @is_not_instream_if_ptr(in_stream) : "The value for 'in_stream' should have been passed as a pointer and not as a value, please add '&'."
105+
@require @is_not_outstream_if_ptr(out_stream) : "The value for 'out_stream' should have been passed as a pointer and not as a value, please add '&'."
106+
@require @is_instream(in_stream) : `The in_stream must implement InStream.`
107+
@require @is_outstream(out_stream) : `The out_stream must implement OutStream.`
108+
@return `The number of bytes written. When a 'limit' is provided and the return value is equal to it, there may be more to read on the current line.`
109+
*>
110+
macro usz? readline_to_stream(out_stream, in_stream = io::stdin(), usz limit = 0)
111+
{
112+
return readline_to_stream_impl{$typeof(in_stream), $typeof(out_stream)}(out_stream, in_stream, limit);
113+
}
114+
115+
fn usz? readline_to_stream_impl(OStream out_stream, IStream in_stream, usz limit) <IStream, OStream> @private
116+
{
117+
bool $is_stream = IStream == InStream;
118+
$if $is_stream:
119+
var func @safeinfer = &in_stream.read_byte;
120+
char val = func((void*)in_stream)!;
121+
$else
122+
char val = in_stream.read_byte()!;
123+
$endif
124+
bool $is_out_stream = OStream == OutStream;
125+
$if $is_out_stream:
126+
var out_func @safeinfer = &out_stream.write_byte;
127+
$endif
128+
129+
if (val == '\n') return 0;
130+
usz len;
131+
if (val != '\r')
132+
{
133+
$if $is_out_stream:
134+
out_func((void*)out_stream.ptr, val)!;
135+
$else
136+
out_stream.write_byte(val)!;
137+
$endif
138+
len++;
139+
}
140+
while (!limit || len < limit)
141+
{
142+
$if $is_stream:
143+
char? c = func((void*)in_stream);
144+
$else
145+
char? c = in_stream.read_byte();
146+
$endif
147+
if (catch err = c)
148+
{
149+
if (err == io::EOF) break;
150+
return err~;
151+
}
152+
if (c == '\r') continue;
153+
if (c == '\n') break;
154+
$if $is_out_stream:
155+
out_func((void*)out_stream.ptr, c)!;
156+
$else
157+
out_stream.write_byte(c)!;
158+
$endif
159+
len++;
160+
}
161+
return len;
162+
}
163+

tests/outputs/io.c3.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
# github.com/AlDanial/cloc
3+
header :
4+
cloc_url : github.com/AlDanial/cloc
5+
cloc_version : 2.09
6+
elapsed_seconds : 0.00584793090820312
7+
n_files : 1
8+
n_lines : 163
9+
files_per_second : 171.000652315721
10+
lines_per_second : 27873.1063274625
11+
report_file : ../outputs/io.c3.yaml
12+
'C3' :
13+
nFiles: 1
14+
blank: 13
15+
comment: 36
16+
code: 114
17+
SUM:
18+
blank: 13
19+
comment: 36
20+
code: 114
21+
nFiles: 1

0 commit comments

Comments
 (0)