Created
March 10, 2024 17:18
-
-
Save fredgrott/501e1a7617cc156dba573dc5cf4664d1 to your computer and use it in GitHub Desktop.
dart behavioral oop, interpreter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2024 Fredrick Allan Grott. All rights reserved. | |
// Use of this source code is governed by a BSD-style | |
// license that can be found in the LICENSE file. | |
import 'package:interpreter/expression.dart'; | |
import 'package:interpreter/number.dart'; | |
class Add implements Expression { | |
@override | |
Number number; | |
Add(this.number); | |
@override | |
void interpret(int value) { | |
print("Adding $value..."); | |
number.value += value; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2024 Fredrick Allan Grott. All rights reserved. | |
// Use of this source code is governed by a BSD-style | |
// license that can be found in the LICENSE file. | |
import 'package:interpreter/number.dart'; | |
abstract class Expression { | |
late Number number; | |
void interpret(int value); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2024 Fredrick Allan Grott. All rights reserved. | |
// Use of this source code is governed by a BSD-style | |
// license that can be found in the LICENSE file. | |
import 'package:interpreter/add.dart'; | |
import 'package:interpreter/aubtract.dart'; | |
import 'package:interpreter/number.dart'; | |
void main() { | |
var number = Number(0); | |
var adder = Add(number); | |
var subtracter = Subtract(number); | |
adder.interpret(100); | |
subtracter.interpret(60); | |
adder.interpret(2); | |
assert(number.value == 42); | |
print("And the result is ${number.value}!"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2024 Fredrick Allan Grott. All rights reserved. | |
// Use of this source code is governed by a BSD-style | |
// license that can be found in the LICENSE file. | |
class Number { | |
late int value; | |
Number(this.value) { | |
print("Starting with $value..."); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2024 Fredrick Allan Grott. All rights reserved. | |
// Use of this source code is governed by a BSD-style | |
// license that can be found in the LICENSE file. | |
import 'package:interpreter/expression.dart'; | |
import 'package:interpreter/number.dart'; | |
class Subtract implements Expression { | |
@override | |
Number number; | |
Subtract(this.number); | |
@override | |
void interpret(int value) { | |
print("Subtracting $value..."); | |
number.value -= value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment