独自のプログラミング言語を作成するこの部分では、以前に定義された構造の拡張としてクラスを実装します。前の部分をチェックしてください: ゼロから独自のプログラミング言語を構築する ゼロから独自のプログラミング言語を構築する: パート II - ダイクストラの 2 スタック アルゴリズム 独自のプログラミング言語を構築する パート III: 正規表現先読みによる字句解析の改善 ゼロから独自のプログラミング言語を構築する パート IV: 関数の実装 独自のプログラミング言語をゼロから構築する: パート V - 配列 ゼロから独自のプログラミング言語を構築する: パート VI - ループ 完全なソースコードが利用可能です GitHubで . 1.字句解析 最初のセクションでは、字句解析について説明します。つまり、ソースコードをキーワード、変数、演算子などの言語語彙素に分割するプロセスです。 前の部分で、すべての語彙素タイプを定義するために 列挙型の正規表現を使用していたことを覚えているかもしれません。 TokenType 次のクラス プロトタイプを見て、不足している正規表現部分を 語彙素に追加してみましょう。 TokenType まず、クラス宣言がどこから始まるかを語彙アナライザーに知らせるために、 単語を 語彙素の式に入れる必要があります。 class Keyword package org.example.toylanguage.token; ... public enum TokenType { ... Keyword("(if|elif|else|end|print|input|fun|return|loop|in|by|break|next|class)(?=\\s|$)"), ... private final String regex; } 次に、現在のオブジェクトへの参照のマーカーとして、新しい 語彙素が必要です。 This public enum TokenType { ... This("(this)(?=,|\\s|$)"); private final String regex; } 2.構文解析 2 番目のセクションでは、語彙アナライザーから受け取った語彙素を、言語規則に従って最終ステートメントに変換します。 2.1 定義範囲 クラスまたは関数を宣言するとき、この宣言は定義された分離境界内で使用できる必要があります。たとえば、次のリストで という名前の関数を宣言すると、宣言後に実行できるようになります。 turn_on [] しかし、クラス スコープ内で同じ関数を宣言すると、この関数はメイン ブロックから直接アクセスできなくなります。 これらの定義境界を実装するには、 クラスを作成し、宣言されたすべての定義をクラスと関数の 2 つのセット内に格納します。 DefinitionScope package org.example.toylanguage.context.definition; public class DefinitionScope { private final Set<ClassDefinition> classes; private final Set<FunctionDefinition> functions; public DefinitionScope() { this.classes = new HashSet<>(); this.functions = new HashSet<>(); } } さらに、親の定義スコープにアクセスしたい場合があります。たとえば、2 つの別個のクラスを宣言し、2 番目のクラス内に最初のクラスのインスタンスを作成するとします。 この機能を提供するために、 親インスタンスを上位層への参照として追加します。これを使用して、最上位の定義層に到達します。 DefinitionScope public class DefinitionScope { private final Set<ClassDefinition> classes; private final Set<FunctionDefinition> functions; private final DefinitionScope parent; public DefinitionScope(DefinitionScope parent) { this.classes = new HashSet<>(); this.functions = new HashSet<>(); this.parent = parent; } } それでは、定義を追加し、親スコープを使用して名前で取得するためのインターフェイスを提供して、実装を完了しましょう。 public class DefinitionScope { … public ClassDefinition getClass(String name) { Optional<ClassDefinition> classDefinition = classes.stream() .filter(t -> t.getName().equals(name)) .findAny(); if (classDefinition.isPresent()) return classDefinition.get(); else if (parent != null) return parent.getClass(name); else throw new ExecutionException(String.format("Class is not defined: %s", name)); } public void addClass(ClassDefinition classDefinition) { classes.add(classDefinition); } public FunctionDefinition getFunction(String name) { Optional<FunctionDefinition> functionDefinition = functions.stream() .filter(t -> t.getName().equals(name)) .findAny(); if (functionDefinition.isPresent()) return functionDefinition.get(); else if (parent != null) return parent.getFunction(name); else throw new ExecutionException(String.format("Function is not defined: %s", name)); } public void addFunction(FunctionDefinition functionDefinition) { functions.add(functionDefinition); } } 最後に、宣言された定義スコープを管理し、それらを切り替えるために、 コレクション (LIFO) を使用してコンテキスト クラスを作成します。 java.util.Stack package org.example.toylanguage.context.definition; public class DefinitionContext { private final static Stack<DefinitionScope> scopes = new Stack<>(); public static DefinitionScope getScope() { return scopes.peek(); } public static DefinitionScope newScope() { return new DefinitionScope(scopes.isEmpty() ? null : scopes.peek()); } public static void pushScope(DefinitionScope scope) { scopes.push(scope); } public static void endScope() { scopes.pop(); } } 2.2 メモリ範囲 このセクションでは、クラス変数と関数変数を管理するための します。 MemoryScope 宣言された各変数は、クラスまたは関数の定義と同様に、分離されたコード ブロック内でのみアクセスできる必要があります。たとえば、次のリストで変数を定義すると、宣言の直後にアクセスできます。 しかし、関数またはクラス内で変数を宣言すると、変数はコードのメイン (上部) ブロックから使用できなくなります。 このロジックを実装し、特定のスコープで定義された変数を格納するには、変数名をキーとして、変数 を値として持つマップを含む クラスを作成します。 Value MemoryScope public class MemoryScope { private final Map<String, Value<?>> variables; public MemoryScope() { this.variables = new HashMap<>(); } } 次に、 と同様に、親のスコープ変数へのアクセスを提供します。 DefinitionScope public class MemoryScope { private final Map<String, Value<?>> variables; private final MemoryScope parent; public MemoryScope(MemoryScope parent) { this.variables = new HashMap<>(); this.parent = parent; } } 次に、変数を取得および設定するメソッドを追加します。変数を設定するとき、上位の レイヤーに既に定義済みの変数がある場合は、常に以前に設定した値を再割り当てします。 MemoryScope public class MemoryScope { ... public Value<?> get(String name) { Value<?> value = variables.get(name); if (value != null) return value; else if (parent != null) return parent.get(name); else return NullValue.NULL_INSTANCE; } public void set(String name, Value<?> value) { MemoryScope variableScope = findScope(name); if (variableScope == null) { variables.put(name, value); } else { variableScope.variables.put(name, value); } } private MemoryScope findScope(String name) { if (variables.containsKey(name)) return this; return parent == null ? null : parent.findScope(name); } } メソッドと メソッドに加えて、 の現在の (ローカル) レイヤーとやり取りするための 2 つの実装を追加します。 set get MemoryScope public class MemoryScope { ... public Value<?> getLocal(String name) { return variables.get(name); } public void setLocal(String name, Value<?> value) { variables.put(name, value); } } これらのメソッドは、後で関数の引数またはクラスのインスタンス引数を初期化するために使用されます。たとえば、 クラスのインスタンスを作成し、定義済みのグローバル 変数を渡す場合、 プロパティを更新しようとするときに、この変数を変更しないでください。 Lamp type lamp_instance :: type 最後に、変数を管理し、メモリ スコープを切り替えるために、 コレクションを使用して 実装を作成します。 java.util.Stack MemoryContext package org.example.toylanguage.context; public class MemoryContext { private static final Stack<MemoryScope> scopes = new Stack<>(); public static MemoryScope getScope() { return scopes.peek(); } public static MemoryScope newScope() { return new MemoryScope(scopes.isEmpty() ? null : scopes.peek()); } public static void pushScope(MemoryScope scope) { scopes.push(scope); } public static void endScope() { scopes.pop(); } } 2.3 クラス定義 このセクションでは、クラス定義を読み取って保存します。 まず、 の実装を作成します。このステートメントは、クラスのインスタンスを作成するたびに実行されます。 Statement package org.example.toylanguage.statement; public class ClassStatement { } 各クラスには、コンストラクターなど、初期化およびその他の操作用のネストされたステートメントを含めることができます。これらのステートメントを保存するために、実行するネストされたステートメントのリストを含む を拡張します。 CompositeStatement public class ClassStatement extends CompositeStatement { } 次に、 を宣言して、クラス名、その引数、コンストラクター ステートメント、およびクラスの関数を含む定義スコープを格納します。 ClassDefinition package org.example.toylanguage.context.definition; import java.util.List; @RequiredArgsConstructor @Getter @EqualsAndHashCode(onlyExplicitlyIncluded = true) public class ClassDefinition implements Definition { @EqualsAndHashCode.Include private final String name; private final List<String> arguments; private final ClassStatement statement; private final DefinitionScope definitionScope; } これで、 を使用してクラス宣言を読み取る準備が整いました。 値を持つ語彙素のキーワードに出会ったら、まずクラス名と角括弧内の引数を読み取る必要があります。 StatementParser class package org.example.toylanguage; public class StatementParser { ... private void parseClassDefinition() { Token type = tokens.next(TokenType.Variable); List<String> arguments = new ArrayList<>(); if (tokens.peek(TokenType.GroupDivider, "[")) { tokens.next(TokenType.GroupDivider, "["); //skip opening square bracket while (!tokens.peek(TokenType.GroupDivider, "]")) { Token argumentToken = tokens.next(TokenType.Variable); arguments.add(argumentToken.getValue()); if (tokens.peek(TokenType.GroupDivider, ",")) tokens.next(); } tokens.next(TokenType.GroupDivider, "]"); //skip closing square bracket } } } 引数の後に、ネストされたコンストラクター ステートメントを読み取る必要があります。 これらのステートメントを保存するために、以前に定義した のインスタンスを作成します。 ClassStatement private void parseClassDefinition() { ... ClassStatement classStatement = new ClassStatement(); } 引数とネストされたステートメントに加えて、クラスには関数を含めることもできます。これらの関数にクラス定義内でのみアクセスできるようにするために、 の新しいレイヤーを初期化します。 DefinitionScope private void parseClassDefinition() { ... ClassStatement classStatement = new ClassStatement(); DefinitionScope classScope = DefinitionContext.newScope(); } 次に、 のインスタンスを初期化し、それを に配置します。 ClassDefinition DefinitionContext private void parseClassDefinition() { ... ClassStatement classStatement = new ClassStatement(); DefinitionScope classScope = DefinitionContext.newScope(); ClassDefinition classDefinition = new ClassDefinition(type.getValue(), arguments, classStatement, classScope); DefinitionContext.getScope().addClass(classDefinition); } 最後に、クラス コンストラクターのステートメントと関数を読み取るために、静的な メソッドを呼び出します。このメソッドは、最後にスキップする必要がある 語彙素に到達するまで、 インスタンス内のステートメントを収集します。 StatementParser#parse() end classStatement private void parseClassDefinition() { ... //parse class statements StatementParser.parse(this, classStatement, classScope); tokens.next(TokenType.Keyword, "end"); } 2.4 クラスインスタンス この時点で、コンストラクター ステートメントと関数を含むクラス定義を既に読み取ることができます。それでは、クラスのインスタンスを解析しましょう。 まず、各クラス インスタンスの状態を含む を定義します。クラスは、関数とは異なり、永続的な を持つ必要があり、このスコープは、クラス インスタンスと対話するたびに、すべてのクラスのインスタンス引数と状態変数で使用できる必要があります。 ClassValue MemoryScope public class ClassValue extends IterableValue<ClassDefinition> { private final MemoryScope memoryScope; public ClassValue(ClassDefinition definition, MemoryScope memoryScope) { super(definition); this.memoryScope = memoryScope; } } 次に、 を使用してクラスのインスタンス プロパティを操作するメソッドを提供します。 MemoryContext public class ClassValue extends IterableValue<ClassDefinition> { private final MemoryScope memoryScope; public ClassValue(ClassDefinition definition, MemoryScope memoryScope) { super(definition); this.memoryScope = memoryScope; } @Override public String toString() { return getValue().getArguments().stream() .map(t -> t + " = " + getValue(t)) .collect(Collectors.joining(", ", getValue().getName() + " [ ", " ]")); } public Value<?> getValue(String name) { Value<?> result = MemoryContext.getScope().getLocal(name); return result != null ? result : NULL_INSTANCE; } public void setValue(String name, Value<?> value) { MemoryContext.getScope().setLocal(name, value); } } および メソッドを呼び出すことで、 変数の現在のレイヤーを操作することに注意してください。ただし、クラスのインスタンス状態にアクセスする前に、その を に配置し、終了したら解放する必要があります。 MemoryScope#getLocal() MemoryScope#setLocal() MemoryScope MemoryScope MemoryContext public class ClassValue extends IterableValue<ClassDefinition> { ... public Value<?> getValue(String name) { MemoryContext.pushScope(memoryScope); try { Value<?> result = MemoryContext.getScope().getLocal(name); return result != null ? result : NULL_INSTANCE; } finally { MemoryContext.endScope(); } } public void setValue(String name, Value<?> value) { MemoryContext.pushScope(memoryScope); try { MemoryContext.getScope().setLocal(name, value); } finally { MemoryContext.endScope(); } } } 次に、構文解析中に定義済みのクラス インスタンスを構築するために使用される残りの を実装できます。クラスのインスタンス定義を宣言するために、ステートメントの実行中に最終的な インスタンスに変換される と 引数のリストを提供します。 ClassExpression Value ClassDefinition Expression package org.example.toylanguage.expression; @RequiredArgsConstructor @Getter public class ClassExpression implements Expression { private final ClassDefinition definition; private final List<Expression> arguments; @Override public Value<?> evaluate() { ... } } 以前に定義された のインスタンスを作成するために実行中に使用される メソッドを実装しましょう。まず、 引数を 引数に評価します。 ClassValue Expression#evaluate() Expression Value @Override public Value<?> evaluate() { //initialize class arguments List<Value<?>> values = arguments.stream() .map(Expression::evaluate) .collect(Collectors.toList()); } 次に、他の変数から分離する必要があり、クラスのインスタンス状態変数のみを含めることができる空のメモリ スコープを作成します。 @Override public Value<?> evaluate() { //initialize class arguments List<Value<?>> values = arguments.stream().map(Expression::evaluate).collect(Collectors.toList()); //get class's definition and statement ClassStatement classStatement = definition.getStatement(); //set separate scope MemoryScope classScope = new MemoryScope(null); MemoryContext.pushScope(classScope); try { ... } finally { MemoryContext.endScope(); } } 次に、 のインスタンスを作成し、クラスの 引数を分離メモリ スコープに書き込みます。 ClassValue Value try { //initialize constructor arguments ClassValue classValue = new ClassValue(definition, classScope); IntStream.range(0, definition.getArguments().size()).boxed() .forEach(i -> MemoryContext.getScope() .setLocal(definition.getArguments().get(i), values.size() > i ? values.get(i) : NullValue.NULL_INSTANCE)); ... } finally { MemoryContext.endScope(); } 空の を設定する前に、 引数を 引数に変換したことに注意してください。そうしないと、クラスのインスタンス引数にアクセスできなくなります。次に例を示します。 MemoryScope Expression Value 最後に、 を実行できます。ただし、その前に、クラスの を設定して、コンストラクター ステートメント内でクラスの関数にアクセスできるようにする必要があります。 ClassStatement DefinitionScope try { //initialize constructor arguments ClassValue classValue = new ClassValue(definition, classScope); ClassInstanceContext.pushValue(classValue); IntStream.range(0, definition.getArguments().size()).boxed() .forEach(i -> MemoryContext.getScope() .setLocal(definition.getArguments().get(i), values.size() > i ? values.get(i) : NullValue.NULL_INSTANCE)); //execute function body DefinitionContext.pushScope(definition.getDefinitionScope()); try { classStatement.execute(); } finally { DefinitionContext.endScope(); } return classValue; } finally { MemoryContext.endScope(); ClassInstanceContext.popValue(); } 9*.最後に、クラスをより柔軟にし、ユーザーがクラス定義を宣言する前にクラス インスタンスを作成できるようにします。 これは、 の初期化を に委任し、式を評価するときにのみアクセスすることで実行できます。 ClassDefinition DefinitionContext public class ClassExpression implements Expression { private final String name; private final List<Expression> arguments; @Override public Value<?> evaluate() { //get class's definition and statement ClassDefinition definition = DefinitionContext.getScope().getClass(name); ... } } FunctionExpression に対して同じ委任を行って、定義の前に を呼び出すことができます。 関数 最後に、 を使用してクラス インスタンスの読み取りを終了できます。 間に違いはありません。 引数を読み取り、 を構築するだけです。 ExpressionReader 以前に定義された構造インスタンス Expression ClassExpression public class ExpressionReader ... // read class instance: new Class[arguments] private ClassExpression readClassInstance(Token token) { List<Expression> arguments = new ArrayList<>(); if (tokens.peekSameLine(TokenType.GroupDivider, "[")) { tokens.next(TokenType.GroupDivider, "["); //skip opening square bracket while (!tokens.peekSameLine(TokenType.GroupDivider, "]")) { Expression value = ExpressionReader.readExpression(this); arguments.add(value); if (tokens.peekSameLine(TokenType.GroupDivider, ",")) tokens.next(); } tokens.next(TokenType.GroupDivider, "]"); //skip closing square bracket } return new ClassExpression(token.getValue(), arguments); } } 2.5 クラス機能 この時点で、クラスを作成し、クラスのコンストラクター ステートメントを実行できます。しかし、まだクラスの機能を実行できません。 関数を呼び出したいクラス インスタンスへの参照として を受け入れる メソッドをオーバーロードしましょう。 ClassValue FunctionExpression#evaluate package org.example.toylanguage.expression; public class FunctionExpression implements Expression { ... public Value<?> evaluate(ClassValue classValue) { } } 次のステップは、現在の を使用して、関数 引数を 引数に変換することです。 MemoryScope Expression Value public Value<?> evaluate(ClassValue classValue) { //initialize function arguments List<Value<?>> values = argumentExpressions.stream() .map(Expression::evaluate) .collect(Collectors.toList()); } 次に、クラスの と をコンテキストに渡す必要があります。 MemoryScope DefinitionScope ... //get definition and memory scopes from class definition ClassDefinition classDefinition = classValue.getValue(); DefinitionScope classDefinitionScope = classDefinition.getDefinitionScope(); //set class's definition and memory scopes DefinitionContext.pushScope(classDefinitionScope); MemoryContext.pushScope(memoryScope); 最後に、この実装では、デフォルトの メソッドを呼び出し、評価された 引数を渡します。 FunctionExpression#evaluate(List<Value<?>> values) Value public Value<?> evaluate(ClassValue classValue) { //initialize function arguments List<Value<?>> values = argumentExpressions.stream().map(Expression::evaluate).collect(Collectors.toList()); //get definition and memory scopes from class definition ClassDefinition classDefinition = classValue.getValue(); DefinitionScope classDefinitionScope = classDefinition.getDefinitionScope(); MemoryScope memoryScope = classValue.getMemoryScope(); //set class's definition and memory scopes DefinitionContext.pushScope(classDefinitionScope); MemoryContext.pushScope(memoryScope); try { //proceed function return evaluate(values); } finally { DefinitionContext.endScope(); MemoryContext.endScope(); } } クラスの関数を呼び出すには、二重コロン 演算子を使用します。現在、この演算子は、クラスのプロパティへのアクセスを担当する ( ) 実装によって管理されています。 :: ClassPropertyOperator StructureValueOperator 同じ二重コロン 文字を使用した関数呼び出しをサポートするように改善しましょう: :: クラスの関数は、左の式が で、2 番目の式が である場合にのみ、この演算子で管理できます。 ClassExpression FunctionExpression package org.example.toylanguage.expression.operator; public class ClassPropertyOperator extends BinaryOperatorExpression implements AssignExpression { public ClassPropertyOperator(Expression left, Expression right) { super(left, right); } @Override public Value<?> evaluate() { Value<?> left = getLeft().evaluate(); if (left instanceof ClassValue) { if (getRight() instanceof VariableExpression) { // access class's property // new ClassInstance[] :: class_argument return ((ClassValue) left).getValue(((VariableExpression) getRight()).getName()); } else if (getRight() instanceof FunctionExpression) { // execute class's function // new ClassInstance[] :: class_function [] return ((FunctionExpression) getRight()).evaluate((ClassValue) left); } } throw new ExecutionException(String.format("Unable to access class's property `%s``", getRight())); } @Override public void assign(Value<?> value) { Value<?> left = getLeft().evaluate(); if (left instanceof ClassValue && getRight() instanceof VariableExpression) { String propertyName = ((VariableExpression) getRight()).getName(); ((ClassValue) left).setValue(propertyName, value); } } } 3. まとめ この部分では、クラスを実装しました。スタック実装など、より複雑なものを作成できるようになりました。 main [] fun main [] stack = new Stack [] loop num in 0..5 # push 0,1,2,3,4 stack :: push [ num ] end size = stack :: size [] loop i in 0..size # should print 4,3,2,1,0 print stack :: pop [] end end class Stack [] arr = {} n = 0 fun push [ item ] this :: arr << item n = n + 1 end fun pop [] n = n - 1 item = arr { n } arr { n } = null return item end fun size [] return this :: n end end