Work on constant string and inferred array sizes.

This commit is contained in:
Christoffer Lerno
2021-02-03 22:28:28 +01:00
committed by Christoffer Lerno
parent 4f064e7da2
commit 1d50beb330
30 changed files with 347 additions and 119 deletions

View File

@@ -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) {

View File

@@ -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;
}

View File

@@ -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);
}

View File

@@ -7,7 +7,7 @@ import stdlib;
const uint MaxText = 1024;
enum TokenKind : char (string name)
enum TokenKind : char (String name)
{
WORD("word"),
TEXT("text"),