Skip to content

Commit

Permalink
added extending to new Parser
Browse files Browse the repository at this point in the history
  • Loading branch information
hg0428 committed Apr 15, 2024
1 parent 1bd64d8 commit 7681ac8
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 9 deletions.
1 change: 1 addition & 0 deletions Aardvark Compiler/Errors/main.adk
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ADK_Error as this {
this.filename = options.filename?
this.location = options.location?
this.initiated_from = options.initiated_from?
this.error_number = options.error_number? -1
this.error = format(
handler.highlighted_code_lines,
options.message?"This isn't valid.",
Expand Down
16 changes: 16 additions & 0 deletions Aardvark Compiler/Run.adk
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
include ADK_Error, ErrorHandler from Errors
include Lexer from Lexer
include Parser from "new-Parser"
include Optimizer

function run_file(filename) {
let code = open(filename).read()
let error_handler = ErrorHandler(code, filename)
let lexer = Lexer(false, false, error_handler)
let tokens = lexer.tokenize(code)
let parser = Parser(lexer, error_handler)
let AST = parser.parse()
# TODO: Cache the AST and tokens


}
49 changes: 40 additions & 9 deletions Aardvark Compiler/new-Parser.adk
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,10 @@ static class Parser as this {
expansions.add(this.parse_expression(require=true))
} else {
let key = parse_typed_variable([], ["NumberLiteral", "StringLiteral", "TemplateString"], "object key")
if type_of(key) == ADK_Error return key
if type_of(key) == ADK_Error {
key.error_number = 1
return key
}
this.eat_line_breaks()
let value
# There is a :, then the value is on the other side
Expand Down Expand Up @@ -852,19 +855,44 @@ static class Parser as this {
position: operator.position
}
}
# TODO
##### TODO!!!!!!
#TODO: handle function call syntax, arrays, objects, and blocks
# stdout.write(object, this.peek(), "\n")
if this.compare("Delimiter", "(") {
if this.compare("Operator", "->") {
let operator = this.eat()
let type = this.parse_primary(require=true)
if !this.is_valid_type(type) {
return this.Throw("SyntaxError", "Invalid syntax.", position=type.position, here_message= $"Expected type.")
}
let body
if this.compare("Delimiter", "{") {
body = this.parse_scope_body()
} else {
body = this.parse_statement(require=true)
}
extension = {
type: "FunctionDefinition",
name: object,
type: type,
parameters: [],
parameter_expansion: null,
is_static: is_static,
is_private: null,
body: body,
position: {
start: object.position.start,
end: body.position.end
}
}
} else if this.compare("Delimiter", "(") {
extension = this.parse_function_definition("extending", is_static)
} else if this.compare("Delimiter", "[") {
extension = this.parse_array()
} else if this.compare("Delimiter", "{") {
this.store_errors = true
extension = this.parse_object()
# if type_of(extension) == ADK_Error
## TODO!!!!!!!! handle object + block scope
if type_of(extension) == ADK_Error & extension.error_number == 1 {
extension = this.parse_scope_body()
}
} else {
extension = this.parse_statement(require=true)
}


Expand Down Expand Up @@ -938,7 +966,10 @@ let parser_tests = {
typed_deconstruction: "let [String a, b, Number c] = x",
object: "{x: 5, y: 6, Number z: 7, d, ...y}",
array_extending: "extending x [5, 6, 7, 8, 9, 10]",
function_extending: "extending x(y, z) -> Number {}"
function_extending: "extending x(y, z) -> Number {}",
function_extending_no_parameters: "extending x {}",
function_extending_no_parameters_with_return_type_annotation: "extending x -> Number {}",
object_extending: "extending x {y: 5, z: 6}",
}
let error_tests = {
assign_value: "let 5 = x",
Expand Down
21 changes: 21 additions & 0 deletions lib/json.adk
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
let json = python.import("json")

let loads = json.loads

let dumps = json.dumps

let dump = json.dump

let decoder = json.decoder

let JSONEncoder = json.JSONEncoder

let JSONDecoder = json.JSONDecoder

let JSONDecodeError = json.JSONDecodeError

let detect_encoding = json.detect_encoding

let scanner = json.scanner

let tool = json.tool

0 comments on commit 7681ac8

Please sign in to comment.