mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
Work on constant string and inferred array sizes.
This commit is contained in:
committed by
Christoffer Lerno
parent
4f064e7da2
commit
1d50beb330
@@ -23,14 +23,14 @@ generic Type[].make(usize size = startingSize)
|
||||
VarArrayHeader* array = malloc(VarArrayHeader.size + Type.size * startingSize);
|
||||
array.capacity = startingSize;
|
||||
array.size = 0;
|
||||
return @cast(array[1] as Type[]);
|
||||
return cast(array[1] as Type[]);
|
||||
}
|
||||
|
||||
macro Type Type[].@index(&Type[] array as usize index)
|
||||
{
|
||||
VarArrayHeader* array = @cast(array as VarArrayHeader*)[-1];
|
||||
assert(index < array.size as "Out of bounds access");
|
||||
return @cast(array as Type *)[index];
|
||||
return cast(array as Type *)[index];
|
||||
}
|
||||
|
||||
foo :: proc($N: $I as $T: typeid) -> (res: [N]T) {
|
||||
|
||||
@@ -1,24 +1,24 @@
|
||||
module levenshtein;
|
||||
|
||||
func int levenshtein(string s, string t)
|
||||
func int levenshtein(String s, String t)
|
||||
{
|
||||
// if either string is empty, difference is inserting all chars
|
||||
// from the other
|
||||
if (!s.size) return t.size;
|
||||
if (!t.size) return s.size;
|
||||
if (!s.len()) return t.len();
|
||||
if (!t.len()) return s.len();
|
||||
|
||||
// if last letters are the same, the difference is whatever is
|
||||
// required to edit the rest of the strings
|
||||
if (s[s.size - 1] == t[t.size - 1]) return levenshtein(s.slice(0, s.size - 1), t.slice(0, t.size - 1));
|
||||
if (s[^1] == t[^1]) return levenshtein(s[0..^2], t[0..^2]);
|
||||
|
||||
// else try:
|
||||
// changing last letter of s to that of t; or
|
||||
// remove last letter of s; or
|
||||
// remove last letter of t,
|
||||
// any of which is 1 edit plus editing the rest of the strings
|
||||
int a = levenshtein(s.slice(0, s.size - 1), t.slice(0, t.size - 1));
|
||||
int b = levenshtein(s, t.slice(0, t.size - 1);
|
||||
int c = levenshtein(s.slice(0, s.size - 1), t);
|
||||
int a = levenshtein(s[0..^2], t[0..^2]);
|
||||
int b = levenshtein(s, t[0..^2]);
|
||||
int c = levenshtein(s[0..^2], t);
|
||||
|
||||
return @max(@max(a, b), c) + 1;
|
||||
}
|
||||
@@ -4,12 +4,12 @@ import regex, stdio;
|
||||
func void main()
|
||||
{
|
||||
println("Enter a story template, terminated by an empty line:");
|
||||
string story = "";
|
||||
String story = "";
|
||||
while (1)
|
||||
{
|
||||
string line = stdin.readln().strip() else "";
|
||||
if (!line.size) break;
|
||||
story += line + "\n";
|
||||
String line = stdin.readln().strip() else break;
|
||||
story = story.append(line);
|
||||
story = story.append("\n");
|
||||
}
|
||||
|
||||
Regex r;
|
||||
@@ -19,9 +19,9 @@ func void main()
|
||||
|
||||
foreach (RegexMatch* match : r.match(story))
|
||||
{
|
||||
string s = match.string;
|
||||
printf("Enter a value for '%s': ", s.slice(1, s.size - 2));
|
||||
string word = strin.readln().strip() else return;
|
||||
String s = match.string;
|
||||
printf("Enter a value for '%s': ", s[1..^2]);
|
||||
string word = stdin.readln().strip() else return;
|
||||
story = story.replace(s, word);
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ import stdlib;
|
||||
|
||||
const uint MaxText = 1024;
|
||||
|
||||
enum TokenKind : char (string name)
|
||||
enum TokenKind : char (String name)
|
||||
{
|
||||
WORD("word"),
|
||||
TEXT("text"),
|
||||
|
||||
Reference in New Issue
Block a user