Skip to content

Commit

Permalink
update to NNBD
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Matthias Aust committed Nov 16, 2020
1 parent 2868784 commit e49a588
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 32 deletions.
10 changes: 5 additions & 5 deletions lib/ast_eval.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Suite {
const Suite(this.stmts);

SmyValue evaluate(Frame f) {
var result = SmyValue.none;
SmyValue result = SmyValue.none;
for (final stmt in stmts) {
result = stmt.evaluate(f);
}
Expand Down Expand Up @@ -125,7 +125,7 @@ class TryExceptStmt extends Stmt {
// TODO search for the right clause
Frame ff = f;
if (except.name != null) {
ff = Frame(f, {SmyString(except.name): ex}, f.globals, f.builtins);
ff = Frame(f, {SmyString(except.name!): ex}, f.globals, f.builtins);
}
except.suite.evaluate(ff);
}
Expand All @@ -135,8 +135,8 @@ class TryExceptStmt extends Stmt {
}

class ExceptClause {
final Expr test;
final String name;
final Expr? test;
final String? name;
final Suite suite;
const ExceptClause(this.test, this.name, this.suite);
}
Expand Down Expand Up @@ -170,7 +170,7 @@ class ClassStmt extends Stmt {
throw 'TypeError: superclass is not a class';
}
final n = SmyString.intern(name);
final cls = SmyClass(n, superclass != SmyValue.none ? superclass : null);
final cls = SmyClass(n, superclass != SmyValue.none ? superclass as SmyClass : null);
f.locals[n] = cls;
suite.evaluate(Frame(f, cls.methods, f.globals, f.builtins));
return SmyValue.none;
Expand Down
6 changes: 3 additions & 3 deletions lib/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class Parser {
// -------- Compount statement parsing --------

// compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
Stmt parseCompoundStmtOpt() {
Stmt? parseCompoundStmtOpt() {
if (at("if")) return parseIfStmt();
if (at("while")) return parseWhileStmt();
if (at("for")) return parseForStmt();
Expand Down Expand Up @@ -233,8 +233,8 @@ class Parser {

// except_clause: 'except' [test ['as' NAME]] ':' suite
ExceptClause _parseExceptClause() {
Expr test;
String name;
Expr? test;
String? name;
if (!at(":")) {
test = parseTest();
if (at("as")) {
Expand Down
32 changes: 16 additions & 16 deletions lib/smython.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ class Smython {
throw 'TypeError: invalid index';
}
if (value is SmyDict) {
return value.values.remove(index);
return value.values.remove(index) ?? SmyValue.none;
}
throw 'TypeError: Unsupported item deletion';
});
}

void builtin(String name, dynamic Function(Frame, List<SmyValue>) func) {
void builtin(String name, SmyValue Function(Frame, List<SmyValue>) func) {
final bname = SmyString.intern(name);
builtins[bname] = SmyBuiltin(bname, func);
}
Expand All @@ -65,9 +65,9 @@ SmyValue make(dynamic value) {
if (value is bool) return value ? True : False;
if (value is int) return SmyInt(value);
if (value is String) return SmyString(value);
if (value is List<dynamic>) return SmyList(value);
if (value is Map<dynamic, dynamic>) return SmyDict(value);
if (value is Set<dynamic>) return SmySet(value);
if (value is List<SmyValue>) return SmyList(value);
if (value is Map<SmyValue, SmyValue>) return SmyDict(value);
if (value is Set<SmyValue>) return SmySet(value);
throw "TypeError: alien value '$value'";
}

Expand Down Expand Up @@ -114,9 +114,9 @@ abstract class SmyValue {

Map<SmyValue, SmyValue> get mapValue => throw 'TypeError: Not a dict';

static const SmyValue none = SmyNone._();
static const SmyValue trueValue = SmyBool(true);
static const SmyValue falseValue = SmyBool(false);
static const SmyNone none = SmyNone._();
static const SmyBool trueValue = SmyBool(true);
static const SmyBool falseValue = SmyBool(false);
}

/// `None` (singleton, equatable, hashable)
Expand Down Expand Up @@ -282,16 +282,16 @@ class SmySet extends SmyValue {
/// `class name (super): ...`
class SmyClass extends SmyValue {
final SmyString _name;
final SmyClass _superclass;
final SmyClass? _superclass;
final SmyDict _dict = SmyDict({});

SmyClass(this._name, this._superclass);

Map<SmyValue, SmyValue> get methods => _dict.values;

SmyValue findAttr(String name) {
SmyValue? findAttr(String name) {
final n = SmyString(name);
for (var cls = this; cls != null; cls = cls._superclass) {
for (SmyClass? cls = this; cls != null; cls = cls._superclass) {
final value = cls._dict.values[n];
if (value != null) return value;
}
Expand Down Expand Up @@ -405,7 +405,7 @@ class SmyBuiltin extends SmyValue {
// -------- Runtime --------

class Frame {
final Frame parent;
final Frame? parent;
final Map<SmyValue, SmyValue> locals;
final Map<SmyValue, SmyValue> globals;
final Map<SmyValue, SmyValue> builtins;
Expand All @@ -414,16 +414,16 @@ class Frame {

SmyValue lookup(SmyString name) {
if (locals.containsKey(name)) {
return locals[name];
return locals[name]!;
}
if (parent != null) {
return parent.lookup(name);
return parent!.lookup(name);
}
if (globals.containsKey(name)) {
return globals[name];
return globals[name]!;
}
if (builtins.containsKey(name)) {
return builtins[name];
return builtins[name]!;
}
throw "NameError: name '$name' is not defined";
}
Expand Down
10 changes: 5 additions & 5 deletions lib/token.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Token {

const Token(this._source, this._start, this._end);

/// Returns the piece of string this token represents.
/// Returns the piece of source code this token represents.
String get value => _source.substring(_start, _end);

/// Returns whether this token is a Smython keyword.
Expand All @@ -25,13 +25,13 @@ class Token {
/// Returns whether this token is a STRING.
bool get isString => _source[_start] == '"' || _source[_start] == '\'';

/// Returns the token's number value (only valid if [isNumber] is true).
/// Returns the token's numeric value (only valid if [isNumber] is true).
int get number => int.parse(value);

/// Returns the token's string value (only valid if [isString] is true).
String get string => _unescape(_source.substring(_start + 1, _end - 1));

/// Returns the line this token is at (1-based).
/// Returns the line of the source code this token is at (1-based).
int get line {
int line = 1;
for (int i = 0; i < _start; i++) {
Expand All @@ -51,13 +51,13 @@ class Token {
static const indent = Token("!INDENT", 0, 7);

static const dedent = Token("!DEDENT", 0, 7);

static const eof = Token("!EOF", 0, 4);

static String _unescape(String s) {
// see scanner.dart for which string escapes are supported
return s.replaceAllMapped(RegExp('\\\\([n\'"\\\\])'), (match) {
final s = match.group(1);
final s = match.group(1)!;
return s == 'n' ? '\n' : s;
});
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
# See https://dart.dev/tools/pub/glossary#lockfile
packages: {}
sdks:
dart: ">=2.4.0 <3.0.0"
dart: ">=2.12.0-0 <3.0.0"
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: smython
description: A subset of Python 3, just large enough to run `fac`.
version: 0.1.1
version: 0.2.0
author: Stefan Matthias Aust <eibaan@gmail.com>
homepage: https://github.com/sma/smythondart/
environment:
sdk: '>=2.4.0 <3.0.0'
sdk: '>=2.12.0-0 <3.0.0'

0 comments on commit e49a588

Please sign in to comment.