make exceptions cooler and fix example code

This commit is contained in:
Gwendolyn 2022-02-13 03:02:23 +01:00
parent d6bdd08002
commit 18d35a5f1e
8 changed files with 196 additions and 172 deletions

View file

@ -2,18 +2,18 @@ import "std:strings";
import "foo" as bar;
import "./foobar";
/*
fn foo() {}
pub fn bar(int x) -> int {}
pub fn bar(x: int): int {}
pub fn multiret() -> int, int {}
pub fn multiret(): int, int {}
[c_export] pub fn add(int a, int b) -> int {}
[c_export] pub fn add(a: int, b: int): int {}
[c_export("add_floats")] pub fn add(float a, float b) -> float {}
[c_export("add_floats")] pub fn add(a: float,b: float): float {}
fn sub<T>(T a, T b) -> T
fn sub<T>(a: T, b: T): T
where SUBTRACT T
{
// do something
@ -21,19 +21,19 @@ fn sub<T>(T a, T b) -> T
fn ret_arr() -> [int; 10] {}
fn ret_slice() -> [int] {}
fn ret_multislice() -> [[int]] {}
fn ret_ptr() -> int* {}
fn ret_ptrslice() -> [int*] {}
fn ret_arrayptr() -> [int; 5]* {}
fn ret_tuple() -> (int, int) {}
fn ret_arr(): [int; 10] {}
fn ret_slice(): [int] {}
fn ret_multislice(): [[int]] {}
fn ret_ptr(): int* {}
fn ret_ptrslice(): [int*] {}
fn ret_arrayptr(): [int; 5]* {}
fn ret_tuple(): (int, int) {}
fn ret_imported() -> strings:StringBuilder {}
fn ret_imported(): strings:StringBuilder {}
fn ret_nested() -> foo@bar {}
fn ret_nested(): foo@bar {}
fn ret_imported_nested() -> foo:bar@some@nested@shit {}
fn ret_imported_nested(): foo:bar@some@nested@shit {}
constraint Foo A {
@ -47,10 +47,7 @@ constraint Bar B {
constraint Foo SomeType<B>;
}
*/
/*
struct Foo {
something: int;
bar: struct {
@ -65,13 +62,13 @@ enum X {
C
}
enum X<T1, T2, T3> {
enum X1<T1, T2, T3> {
A(T1),
B(T2),
C(T3),
}
enum X<T1, T2, T3> where Constraint T1 T2 where Constraint T2 T3 {
enum X2<T1, T2, T3> where Constraint T1 T2 where Constraint T2 T3 {
A(T1),
B(T2),
C(T3),
@ -91,14 +88,12 @@ type F<X> = Foo<Bar<X>>;
type X = [Y; 10];
type Xa = [Y; 10];
*/
/*
tuple X<A> (A,A,A);
tuple Xa<A> (A,A,A);
type X<T> = [T; 10];
type Xaa<T> = [T; 10];
type X<T1, T2> = (T1, T2);
fn foo() {
@ -132,7 +127,7 @@ struct Bar {
}
struct Foo {
struct Fooo {
x: enum {Foo, Bar};
}
@ -175,15 +170,13 @@ fn test() {
type A = int*;
type Aaaaa = int*;
struct B {
x: (int, int);
}*/
}
type A<T> = [(T,int); 10];
struct A<T> {
struct Aaaaa<T> {
x: B<T>;
y: s32;
}

View file

@ -25,7 +25,7 @@ public class ExpressionVisitorNya : MeowBaseVisitorNya<ExpressionModel>
if (expressions.Count == 1) return expressions[0];
var left = MakeBinOpTree(expressions.Take(expressions.Count - 1).ToList(), op);
var right = expressions[^1];
var span = new Span(left.Span.Filename, left.Span.From, right.Span.To);
var span = new Span(left.Span.Filename, left.Span.From, right.Span.To, left.Span.Line);
return new BinaryOperationExpressionModel(span, left, op, right);
}
@ -37,7 +37,7 @@ public class ExpressionVisitorNya : MeowBaseVisitorNya<ExpressionModel>
var remainingExpressions = expressions.Take(expressions.Count - 1).ToList();
var remainingOps = ops.Take(ops.Count - 1).ToList();
var left = MakeBinOpTree(remainingExpressions, remainingOps);
var span = new Span(left.Span.Filename, left.Span.From, right.Span.To);
var span = new Span(left.Span.Filename, left.Span.From, right.Span.To, left.Span.Line);
return new BinaryOperationExpressionModel(span, left, op, right);
}
@ -117,7 +117,7 @@ public class ExpressionVisitorNya : MeowBaseVisitorNya<ExpressionModel>
var expression = Visit(context.pointerDereferenceExpression());
foreach (var unaryOperator in context._op.Reverse())
{
var span = new Span(expression.Span.Filename, unaryOperator.StartIndex, expression.Span.To);
var span = new Span(expression.Span.Filename, unaryOperator.StartIndex, expression.Span.To, expression.Span.Line);
if (unaryOperator.Text == "&")
{
expression = new PointerReferenceExpressionModel(span, expression);
@ -137,7 +137,7 @@ public class ExpressionVisitorNya : MeowBaseVisitorNya<ExpressionModel>
var expression = Visit(context.accessExpression());
foreach (var op in context._op.Reverse())
{
var span = new Span(expression.Span.Filename, op.StartIndex, expression.Span.To);
var span = new Span(expression.Span.Filename, op.StartIndex, expression.Span.To, expression.Span.Line);
expression = new PointerDereferenceExpressionModel(span, expression);
}
@ -152,13 +152,13 @@ public class ExpressionVisitorNya : MeowBaseVisitorNya<ExpressionModel>
{
if (current.subscript != null) // array access
{
var span = new Span(expression.Span.Filename, expression.Span.From, current.end.StopIndex);
var span = new Span(expression.Span.Filename, expression.Span.From, current.end.StopIndex, expression.Span.Line);
var indexer = Visit(current.subscript);
expression = new ArrayAccessExpressionModel(span, expression, indexer);
}
else // struct access
{
var span = new Span(expression.Span.Filename, expression.Span.From, current.name.StopIndex);
var span = new Span(expression.Span.Filename, expression.Span.From, current.name.StopIndex, expression.Span.Line);
var name = current.name.Text;
if (name.Contains('.') || (char.IsDigit(name[0]) && !char.IsDigit(name.TrimStart('0')[0])))
{

View file

@ -20,6 +20,7 @@ public class MeowModelVisitor<T>
public T Visit(ModelBase m)
{
try {
switch(m)
{
@ -84,6 +85,11 @@ public class MeowModelVisitor<T>
case VariableDeclarationStatementModel x: return VisitVariableDeclarationStatement(x);
case WhileLoopStatementModel x: return VisitWhileLoopStatement(x);
}
} catch(Exception e)
{
if (e is MeowModelVisitorException) throw;
throw new MeowModelVisitorException(m, e);
}
return default;
}
@ -754,6 +760,7 @@ public class MeowModelVisitor
{
public void Visit(ModelBase m)
{
try {
switch(m)
{
@ -818,6 +825,9 @@ public class MeowModelVisitor
case VariableDeclarationStatementModel x: VisitVariableDeclarationStatement(x); break;
case WhileLoopStatementModel x: VisitWhileLoopStatement(x); break;
}
} catch(Exception e) {
throw new MeowModelVisitorException(m, e);
}
}
public virtual void VisitArrayAccessExpression(ArrayAccessExpressionModel m)

View file

@ -0,0 +1,11 @@
namespace meowlang.parser;
public class MeowModelVisitorException : Exception
{
public ModelBase Model { get; }
public MeowModelVisitorException(ModelBase model, Exception inner) : base($"{inner.Message} on line {model.Span.Line}", inner)
{
Model = model;
}
}

View file

@ -6,7 +6,7 @@ public class MeowVisitorException : Exception
{
public IToken Token { get; }
public MeowVisitorException(IToken token, string message) : base(message)
public MeowVisitorException(IToken token, string message) : base($"{message} on line {token.Line}")
{
Token = token;
}

View file

@ -1,3 +1,3 @@
namespace meowlang.parser;
public record Span(string Filename, int From, int To);
public record Span(string Filename, int From, int To, int Line);

View file

@ -118,7 +118,7 @@ internal static class ParserRuleContextExtensions
{
public static Span GetSpan(this ParserRuleContext context)
{
return new Span(context.Start.TokenSource.SourceName, context.Start.StartIndex, context.Stop.StopIndex);
return new Span(context.Start.TokenSource.SourceName, context.Start.StartIndex, context.Stop.StopIndex, context.Start.Line);
}
}
@ -126,7 +126,7 @@ internal static class TokenExtensions
{
public static Span GetSpan(this IToken token)
{
return new Span(token.TokenSource.SourceName, token.StartIndex, token.StopIndex);
return new Span(token.TokenSource.SourceName, token.StartIndex, token.StopIndex, token.Line);
}
}

View file

@ -97,10 +97,15 @@ public class {className}<T>
public T Visit(ModelBase m)
{{
try {{
switch(m)
{{
{string.Join("", arms)}
}}
}} catch(Exception e) {{
if (e is MeowModelVisitorException) throw;
throw new MeowModelVisitorException(m, e);
}}
return default;
}}
{string.Join("", methods)}
@ -111,10 +116,15 @@ public class {className}
{{
public void Visit(ModelBase m)
{{
try {{
switch(m)
{{
{string.Join("", voidArms)}
}}
}} catch(Exception e) {{
if (e is MeowModelVisitorException) throw;
throw new MeowModelVisitorException(m, e);
}}
}}
{string.Join("", voidMethods)}
}}