Deprecate !!
print statement
This commit is contained in:
parent
95633b1cc2
commit
59845e610f
@ -138,7 +138,7 @@ func load_routes(directory:Path -> {Text=RouteEntry}):
|
||||
func main(directory:Path, port=Int32(8080)):
|
||||
say("Serving on port $port")
|
||||
routes := load_routes(directory)
|
||||
!! Hosting: $routes
|
||||
say(" Hosting: $routes")
|
||||
|
||||
serve(port, func(request:HTTPRequest):
|
||||
if handler := routes[request.path]:
|
||||
|
@ -97,17 +97,17 @@ func delete(url:Text, data:Text?=none, headers=["Content-Type: application/json"
|
||||
return _send(DELETE, url, data, headers)
|
||||
|
||||
func main():
|
||||
!! GET:
|
||||
say("GET:")
|
||||
say(get("https://httpbin.org/get").body)
|
||||
!! Waiting 1sec...
|
||||
say("Waiting 1sec...
|
||||
sleep(1)
|
||||
!! POST:
|
||||
say("POST:")
|
||||
say(post("https://httpbin.org/post", `{"key": "value"}`).body)
|
||||
!! Waiting 1sec...
|
||||
say("Waiting 1sec...")
|
||||
sleep(1)
|
||||
!! PUT:
|
||||
say("PUT:")
|
||||
say(put("https://httpbin.org/put", `{"key": "value"}`).body)
|
||||
!! Waiting 1sec...
|
||||
say("Waiting 1sec...")
|
||||
sleep(1)
|
||||
!! DELETE:
|
||||
say("DELETE:")
|
||||
say(delete("https://httpbin.org/delete").body)
|
||||
|
@ -41,7 +41,7 @@ func main(path:Path, key:Text?):
|
||||
|
||||
data := parse_ini(path)
|
||||
if keys.length < 1 or keys[1] == '*':
|
||||
!! $data
|
||||
say("$data")
|
||||
return
|
||||
|
||||
section := keys[1]:lower()
|
||||
@ -50,7 +50,7 @@ func main(path:Path, key:Text?):
|
||||
Valid names: $\[1]$(", ":join([k:quoted() for k in data.keys]))$\[]
|
||||
")
|
||||
if keys.length < 2 or keys[2] == '*':
|
||||
!! $section_data
|
||||
say("$section_data")
|
||||
return
|
||||
|
||||
section_key := keys[2]:lower()
|
||||
|
@ -9,9 +9,6 @@ func main():
|
||||
# Print to the console
|
||||
say("Hello world!")
|
||||
|
||||
# You can also use !! as a shorthand:
|
||||
!! This is the same as using say(), but a bit easier to type
|
||||
|
||||
# Declare a variable with ':=' (the type is inferred to be integer)
|
||||
my_variable := 123
|
||||
|
||||
|
@ -13,7 +13,7 @@ enum Dependency(File(path:Path), Module(name:Text))
|
||||
|
||||
func _get_file_dependencies(file:Path -> {Dependency}):
|
||||
if not file:is_file():
|
||||
!! Could not read file: $file
|
||||
say("Could not read file: $file")
|
||||
return {}
|
||||
|
||||
deps : @{Dependency} = @{}
|
||||
|
@ -177,7 +177,6 @@ CORD ast_to_xml(ast_t *ast)
|
||||
optional_tagged("iterable", data.iter))
|
||||
T(Skip, "<Skip>%r</Skip>", data.target)
|
||||
T(Stop, "<Stop>%r</Stop>", data.target)
|
||||
T(PrintStatement, "<PrintStatement>%r</PrintStatement>", ast_list_to_xml(data.to_print))
|
||||
T(Pass, "<Pass/>")
|
||||
T(Defer, "<Defer>%r<Defer/>", ast_to_xml(data.body))
|
||||
T(Return, "<Return>%r</Return>", ast_to_xml(data.value))
|
||||
|
@ -123,7 +123,7 @@ typedef enum {
|
||||
Unknown = 0,
|
||||
None, Bool, Var,
|
||||
Int, Num,
|
||||
TextLiteral, TextJoin, PrintStatement,
|
||||
TextLiteral, TextJoin,
|
||||
Path,
|
||||
Declare, Assign,
|
||||
Power, Multiply, Divide, Mod, Mod1, Plus, Minus, Concat, LeftShift, UnsignedLeftShift,
|
||||
@ -182,9 +182,6 @@ struct ast_s {
|
||||
struct {
|
||||
const char *path;
|
||||
} Path;
|
||||
struct {
|
||||
ast_list_t *to_print;
|
||||
} PrintStatement;
|
||||
struct {
|
||||
ast_t *var;
|
||||
type_ast_t *type;
|
||||
|
@ -213,11 +213,6 @@ static void add_closed_vars(Table_t *closed_vars, env_t *enclosing_scope, env_t
|
||||
add_closed_vars(closed_vars, enclosing_scope, env, child->ast);
|
||||
break;
|
||||
}
|
||||
case PrintStatement: {
|
||||
for (ast_list_t *child = Match(ast, PrintStatement)->to_print; child; child = child->next)
|
||||
add_closed_vars(closed_vars, enclosing_scope, env, child->ast);
|
||||
break;
|
||||
}
|
||||
case Declare: {
|
||||
add_closed_vars(closed_vars, enclosing_scope, env, Match(ast, Declare)->value);
|
||||
bind_statement(env, ast);
|
||||
@ -1406,24 +1401,6 @@ static CORD _compile_statement(env_t *env, ast_t *ast)
|
||||
env->deferred = new(deferral_t, .defer_env=defer_env, .block=body, .next=env->deferred);
|
||||
return code;
|
||||
}
|
||||
case PrintStatement: {
|
||||
ast_list_t *to_print = Match(ast, PrintStatement)->to_print;
|
||||
if (!to_print)
|
||||
return CORD_EMPTY;
|
||||
|
||||
CORD code = "say(";
|
||||
if (to_print->next) code = CORD_all(code, "Texts(");
|
||||
for (ast_list_t *chunk = to_print; chunk; chunk = chunk->next) {
|
||||
if (chunk->ast->tag == TextLiteral) {
|
||||
code = CORD_cat(code, compile(env, chunk->ast));
|
||||
} else {
|
||||
code = CORD_cat(code, compile_string(env, chunk->ast, "USE_COLOR"));
|
||||
}
|
||||
if (chunk->next) code = CORD_cat(code, ", ");
|
||||
}
|
||||
if (to_print->next) code = CORD_all(code, ")");
|
||||
return CORD_cat(code, ", yes);");
|
||||
}
|
||||
case Return: {
|
||||
if (!env->fn_ret) code_err(ast, "This return statement is not inside any function");
|
||||
auto ret = Match(ast, Return)->value;
|
||||
@ -3738,7 +3715,7 @@ CORD compile(env_t *env, ast_t *ast)
|
||||
case Extern: code_err(ast, "Externs are not supported as expressions");
|
||||
case TableEntry: code_err(ast, "Table entries should not be compiled directly");
|
||||
case Declare: case Assign: case UPDATE_CASES: case For: case While: case Repeat: case StructDef: case LangDef: case Extend:
|
||||
case EnumDef: case FunctionDef: case ConvertDef: case Skip: case Stop: case Pass: case Return: case DocTest: case PrintStatement:
|
||||
case EnumDef: case FunctionDef: case ConvertDef: case Skip: case Stop: case Pass: case Return: case DocTest:
|
||||
code_err(ast, "This is not a valid expression");
|
||||
default: case Unknown: code_err(ast, "Unknown AST: ", ast_to_xml_str(ast));
|
||||
}
|
||||
|
46
src/parse.c
46
src/parse.c
@ -131,7 +131,6 @@ static PARSER(parse_path);
|
||||
static PARSER(parse_reduction);
|
||||
static PARSER(parse_repeat);
|
||||
static PARSER(parse_return);
|
||||
static PARSER(parse_say);
|
||||
static PARSER(parse_set);
|
||||
static PARSER(parse_skip);
|
||||
static PARSER(parse_stack_reference);
|
||||
@ -1740,8 +1739,7 @@ PARSER(parse_assignment) {
|
||||
PARSER(parse_statement) {
|
||||
ast_t *stmt = NULL;
|
||||
if ((stmt=parse_declaration(ctx, pos))
|
||||
|| (stmt=parse_doctest(ctx, pos))
|
||||
|| (stmt=parse_say(ctx, pos)))
|
||||
|| (stmt=parse_doctest(ctx, pos)))
|
||||
return stmt;
|
||||
|
||||
if (!(false
|
||||
@ -2287,48 +2285,6 @@ PARSER(parse_doctest) {
|
||||
return NewAST(ctx->file, start, pos, DocTest, .expr=expr, .expected=expected);
|
||||
}
|
||||
|
||||
PARSER(parse_say) {
|
||||
const char *start = pos;
|
||||
if (!match(&pos, "!!")) return NULL;
|
||||
spaces(&pos);
|
||||
|
||||
ast_list_t *chunks = NULL;
|
||||
CORD chunk = CORD_EMPTY;
|
||||
const char *chunk_start = pos;
|
||||
const char open_interp = '$';
|
||||
while (pos < ctx->file->text + ctx->file->len) {
|
||||
if (*pos == open_interp) { // Interpolation
|
||||
const char *interp_start = pos;
|
||||
if (chunk) {
|
||||
ast_t *literal = NewAST(ctx->file, chunk_start, pos, TextLiteral, .cord=chunk);
|
||||
chunks = new(ast_list_t, .ast=literal, .next=chunks);
|
||||
chunk = NULL;
|
||||
}
|
||||
++pos;
|
||||
ast_t *interp;
|
||||
if (*pos == ' ' || *pos == '\t')
|
||||
parser_err(ctx, pos, pos+1, "Whitespace is not allowed before an interpolation here");
|
||||
interp = expect(ctx, interp_start, &pos, parse_term, "I expected an interpolation term here");
|
||||
chunks = new(ast_list_t, .ast=interp, .next=chunks);
|
||||
chunk_start = pos;
|
||||
} else if (*pos == '\r' || *pos == '\n') { // Newline
|
||||
break;
|
||||
} else { // Plain character
|
||||
chunk = CORD_cat_char(chunk, *pos);
|
||||
++pos;
|
||||
}
|
||||
}
|
||||
|
||||
if (chunk) {
|
||||
ast_t *literal = NewAST(ctx->file, chunk_start, pos, TextLiteral, .cord=chunk);
|
||||
chunks = new(ast_list_t, .ast=literal, .next=chunks);
|
||||
chunk = NULL;
|
||||
}
|
||||
|
||||
REVERSE_LIST(chunks);
|
||||
return NewAST(ctx->file, start, pos, PrintStatement, .to_print=chunks);
|
||||
}
|
||||
|
||||
PARSER(parse_use) {
|
||||
const char *start = pos;
|
||||
|
||||
|
@ -1020,7 +1020,7 @@ type_t *get_type(env_t *env, ast_t *ast)
|
||||
case Stop: case Skip: {
|
||||
return Type(AbortType);
|
||||
}
|
||||
case Pass: case Defer: case PrintStatement: return Type(VoidType);
|
||||
case Pass: case Defer: return Type(VoidType);
|
||||
case Negative: {
|
||||
ast_t *value = Match(ast, Negative)->value;
|
||||
type_t *t = get_type(env, value);
|
||||
|
@ -1,6 +1,6 @@
|
||||
|
||||
func main():
|
||||
!! Test bytes:
|
||||
say("Test bytes:")
|
||||
>> Byte(100)
|
||||
= Byte(0x64)
|
||||
|
||||
|
@ -17,7 +17,7 @@ func main():
|
||||
>> 1 << 10
|
||||
= 1024
|
||||
|
||||
!! Signed and unsigned bit shifting:
|
||||
say("Signed and unsigned bit shifting:")
|
||||
>> Int64(-2) << 1
|
||||
= Int64(-4)
|
||||
>> Int64(-2) <<< 1
|
||||
|
@ -36,7 +36,7 @@ func main():
|
||||
fn := func():
|
||||
return func():
|
||||
return func():
|
||||
defer: !! $outer
|
||||
defer: say("$outer")
|
||||
return outer
|
||||
>> fn()()()
|
||||
= "Hello"
|
||||
|
@ -86,7 +86,7 @@ func main():
|
||||
= -1
|
||||
|
||||
do:
|
||||
!! Ints:
|
||||
say("Ints:")
|
||||
>> yep := maybe_int(yes)
|
||||
= 123?
|
||||
>> nope := maybe_int(no)
|
||||
@ -97,11 +97,10 @@ func main():
|
||||
else: fail("Falsey: $yep")
|
||||
>> if nope:
|
||||
fail("Truthy: $nope")
|
||||
else: !! Falsey: $nope
|
||||
else: say("Falsey: $nope")
|
||||
|
||||
do:
|
||||
!! ...
|
||||
!! Int64s:
|
||||
say("Int64s:")
|
||||
>> yep := maybe_int64(yes)
|
||||
= Int64(123)?
|
||||
>> nope := maybe_int64(no)
|
||||
@ -112,11 +111,10 @@ func main():
|
||||
else: fail("Falsey: $yep")
|
||||
>> if nope:
|
||||
fail("Truthy: $nope")
|
||||
else: !! Falsey: $nope
|
||||
else: say("Falsey: $nope")
|
||||
|
||||
do:
|
||||
!! ...
|
||||
!! Arrays:
|
||||
say("Arrays:")
|
||||
>> yep := maybe_array(yes)
|
||||
= [10, 20, 30]?
|
||||
>> nope := maybe_array(no)
|
||||
@ -127,11 +125,11 @@ func main():
|
||||
else: fail("Falsey: $yep")
|
||||
>> if nope:
|
||||
fail("Truthy: $nope")
|
||||
else: !! Falsey: $nope
|
||||
else: say("Falsey: $nope")
|
||||
|
||||
do:
|
||||
!! ...
|
||||
!! Bools:
|
||||
say("...")
|
||||
say("Bools:")
|
||||
>> yep := maybe_bool(yes)
|
||||
= no?
|
||||
>> nope := maybe_bool(no)
|
||||
@ -142,11 +140,11 @@ func main():
|
||||
else: fail("Falsey: $yep")
|
||||
>> if nope:
|
||||
fail("Truthy: $nope")
|
||||
else: !! Falsey: $nope
|
||||
else: say("Falsey: $nope")
|
||||
|
||||
do:
|
||||
!! ...
|
||||
!! Text:
|
||||
say("...")
|
||||
say("Text:")
|
||||
>> yep := maybe_text(yes)
|
||||
= "Hello"?
|
||||
>> nope := maybe_text(no)
|
||||
@ -157,11 +155,11 @@ func main():
|
||||
else: fail("Falsey: $yep")
|
||||
>> if nope:
|
||||
fail("Truthy: $nope")
|
||||
else: !! Falsey: $nope
|
||||
else: say("Falsey: $nope")
|
||||
|
||||
do:
|
||||
!! ...
|
||||
!! Nums:
|
||||
say("...")
|
||||
say("Nums:")
|
||||
>> yep := maybe_num(yes)
|
||||
= 12.3?
|
||||
>> nope := maybe_num(no)
|
||||
@ -172,11 +170,11 @@ func main():
|
||||
else: fail("Falsey: $yep")
|
||||
>> if nope:
|
||||
fail("Truthy: $nope")
|
||||
else: !! Falsey: $nope
|
||||
else: say("Falsey: $nope")
|
||||
|
||||
do:
|
||||
!! ...
|
||||
!! Lambdas:
|
||||
say("...")
|
||||
say("Lambdas:")
|
||||
# >> yep := maybe_lambda(yes)
|
||||
# = func() [optionals.tm:54] : func()?
|
||||
>> nope := maybe_lambda(no)
|
||||
@ -187,11 +185,11 @@ func main():
|
||||
# else: fail("Falsey: $yep")
|
||||
>> if nope:
|
||||
fail("Truthy: $nope")
|
||||
else: !! Falsey: $nope
|
||||
else: say("Falsey: $nope")
|
||||
|
||||
do:
|
||||
!! ...
|
||||
!! Structs:
|
||||
say("...")
|
||||
say("Structs:")
|
||||
>> yep := Struct.maybe(yes)
|
||||
= Struct(x=123, y="hello")?
|
||||
>> nope := Struct.maybe(no)
|
||||
@ -202,11 +200,11 @@ func main():
|
||||
else: fail("Falsey: $yep")
|
||||
>> if nope:
|
||||
fail("Truthy: $nope")
|
||||
else: !! Falsey: $nope
|
||||
else: say("Falsey: $nope")
|
||||
|
||||
do:
|
||||
!! ...
|
||||
!! Enums:
|
||||
say("...")
|
||||
say("Enums:")
|
||||
>> yep := Enum.maybe(yes)
|
||||
= Enum.Y(123)?
|
||||
>> nope := Enum.maybe(no)
|
||||
@ -217,11 +215,11 @@ func main():
|
||||
else: fail("Falsey: $yep")
|
||||
>> if nope:
|
||||
fail("Truthy: $nope")
|
||||
else: !! Falsey: $nope
|
||||
else: say("Falsey: $nope")
|
||||
|
||||
do:
|
||||
!! ...
|
||||
!! C Strings:
|
||||
say("...")
|
||||
say("C Strings:")
|
||||
>> yep := maybe_c_string(yes)
|
||||
= CString("hi")?
|
||||
>> nope := maybe_c_string(no)
|
||||
@ -232,7 +230,7 @@ func main():
|
||||
else: fail("Falsey: $yep")
|
||||
>> if nope:
|
||||
fail("Truthy: $nope")
|
||||
else: !! Falsey: $nope
|
||||
else: say("Falsey: $nope")
|
||||
|
||||
if yep := maybe_int(yes):
|
||||
>> yep
|
||||
|
@ -82,49 +82,49 @@ func main():
|
||||
= (..)
|
||||
|
||||
# Concatenation tests:
|
||||
!! Basic relative path concatenation:
|
||||
say("Basic relative path concatenation:")
|
||||
>> (/foo) ++ (./baz)
|
||||
= (/foo/baz)
|
||||
|
||||
!! Concatenation with a current directory (`.`):
|
||||
say("Concatenation with a current directory (`.`):")
|
||||
>> (/foo/bar) ++ (./.)
|
||||
= (/foo/bar)
|
||||
|
||||
!! Trailing slash in the first path:
|
||||
say("Trailing slash in the first path:")
|
||||
>> (/foo/) ++ (./baz)
|
||||
= (/foo/baz)
|
||||
|
||||
!! Trailing slash in the second path:
|
||||
say("Trailing slash in the second path:")
|
||||
>> (/foo/bar) ++ (./baz/)
|
||||
= (/foo/bar/baz)
|
||||
|
||||
!! Removing redundant current directory (`.`):
|
||||
say("Removing redundant current directory (`.`):")
|
||||
>> (/foo/bar) ++ (./baz/./qux)
|
||||
= (/foo/bar/baz/qux)
|
||||
|
||||
!! Removing redundant parent directory (`..`):
|
||||
say("Removing redundant parent directory (`..`):")
|
||||
>> (/foo/bar) ++ (./baz/qux/../quux)
|
||||
= (/foo/bar/baz/quux)
|
||||
|
||||
!! Collapsing `..` to navigate up:
|
||||
say("Collapsing `..` to navigate up:")
|
||||
>> (/foo/bar/baz) ++ (../qux)
|
||||
= (/foo/bar/qux)
|
||||
|
||||
!! Current directory and parent directory mixed:
|
||||
say("Current directory and parent directory mixed:")
|
||||
>> (/foo/bar) ++ (././../baz)
|
||||
= (/foo/baz)
|
||||
|
||||
!! Path begins with a `.`:
|
||||
say("Path begins with a `.`:")
|
||||
>> (/foo) ++ (./baz/../qux)
|
||||
= (/foo/qux)
|
||||
|
||||
!! Multiple slashes:
|
||||
say("Multiple slashes:")
|
||||
>> (/foo) ++ (./baz//qux)
|
||||
= (/foo/baz/qux)
|
||||
|
||||
!! Complex path with multiple `.` and `..`:
|
||||
say("Complex path with multiple `.` and `..`:")
|
||||
>> (/foo/bar/baz) ++ (./.././qux/./../quux)
|
||||
= (/foo/bar/quux)
|
||||
|
||||
!! Globbing:
|
||||
say("Globbing:")
|
||||
>> (./*.tm):glob()
|
||||
|
@ -27,7 +27,7 @@ func main():
|
||||
>> (_max_.y:abs(): [Foo(0, 0), Foo(1, 0), Foo(0, 10), Foo(0, -999)])!
|
||||
= Foo(x=0, y=-999)
|
||||
|
||||
!! (or) and (and) have early out behavior:
|
||||
say("(or) and (and) have early out behavior:")
|
||||
>> (or: i == 3 for i in 9999999999999999999999999999)!
|
||||
= yes
|
||||
|
||||
|
10
test/text.tm
10
test/text.tm
@ -1,6 +1,6 @@
|
||||
func main():
|
||||
str := "Hello Amélie!"
|
||||
!! Testing strings like $str
|
||||
say("Testing strings like $str")
|
||||
|
||||
>> str:upper()
|
||||
= "HELLO AMÉLIE!"
|
||||
@ -100,7 +100,7 @@ func main():
|
||||
"
|
||||
= "line one$\nline two"
|
||||
|
||||
!! Interpolation tests:
|
||||
say("Interpolation tests:")
|
||||
>> "A $(1+2)"
|
||||
= "A 3"
|
||||
>> 'A $(1+2)'
|
||||
@ -138,7 +138,7 @@ func main():
|
||||
>> "":lines()
|
||||
= []
|
||||
|
||||
!! Test splitting and joining text:
|
||||
say("Test splitting and joining text:")
|
||||
>> "one,, two,three":split(",")
|
||||
= ["one", "", " two", "three"]
|
||||
>> [t for t in "one,, two,three":by_split(",")]
|
||||
@ -177,7 +177,7 @@ func main():
|
||||
>> "":split()
|
||||
= []
|
||||
|
||||
!! Test text slicing:
|
||||
say("Test text slicing:")
|
||||
>> "abcdef":slice()
|
||||
= "abcdef"
|
||||
>> "abcdef":slice(from=3)
|
||||
@ -237,7 +237,7 @@ func main():
|
||||
= "eilémA olleh"
|
||||
|
||||
do:
|
||||
!! Testing concatenation-stability:
|
||||
say("Testing concatenation-stability:")
|
||||
ab := Text.from_codepoint_names(["LATIN SMALL LETTER E", "COMBINING VERTICAL LINE BELOW"])!
|
||||
>> ab:codepoint_names()
|
||||
= ["LATIN SMALL LETTER E", "COMBINING VERTICAL LINE BELOW"]
|
||||
|
Loading…
Reference in New Issue
Block a user