classes
This commit is contained in:
parent
c516afc400
commit
08c95294f6
5 changed files with 97 additions and 4 deletions
|
|
@ -3,4 +3,5 @@
|
||||||
- no lambdas
|
- no lambdas
|
||||||
- tuples need to be in ()
|
- tuples need to be in ()
|
||||||
- converts tuples into lists
|
- converts tuples into lists
|
||||||
- no += -= *= /= ...
|
- no default function argument values
|
||||||
|
- no expressions of type l_bound < value < r_bound
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "ast.hpp"
|
#include "ast.hpp"
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <set>
|
||||||
|
|
||||||
// greatly simplified
|
// greatly simplified
|
||||||
std::string convert_string(std::string input)
|
std::string convert_string(std::string input)
|
||||||
|
|
@ -28,6 +29,7 @@ class RubyGenerator
|
||||||
std::ostringstream output;
|
std::ostringstream output;
|
||||||
int indent_level = 0;
|
int indent_level = 0;
|
||||||
bool write_contains = false;
|
bool write_contains = false;
|
||||||
|
std::set<std::string> class_names;
|
||||||
|
|
||||||
void write_indent()
|
void write_indent()
|
||||||
{
|
{
|
||||||
|
|
@ -215,6 +217,10 @@ private:
|
||||||
{
|
{
|
||||||
output << "def initialize";
|
output << "def initialize";
|
||||||
}
|
}
|
||||||
|
else if (function_def->name == "__str__")
|
||||||
|
{
|
||||||
|
output << "def to_s";
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
output << "def " << function_def->name << "";
|
output << "def " << function_def->name << "";
|
||||||
|
|
@ -259,6 +265,7 @@ private:
|
||||||
}
|
}
|
||||||
else if (auto *class_def = dynamic_cast<const ClassDef *>(&stmt))
|
else if (auto *class_def = dynamic_cast<const ClassDef *>(&stmt))
|
||||||
{
|
{
|
||||||
|
class_names.insert(class_def->name);
|
||||||
write_indent();
|
write_indent();
|
||||||
output << "class " << class_def->name << "\n";
|
output << "class " << class_def->name << "\n";
|
||||||
indent_level++;
|
indent_level++;
|
||||||
|
|
@ -431,6 +438,19 @@ private:
|
||||||
write(", \"\\n\")");
|
write(", \"\\n\")");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
else if (class_names.count(func_id->name))
|
||||||
|
{
|
||||||
|
write(func_id->name);
|
||||||
|
write(".new(");
|
||||||
|
for (size_t i = 0; i < call->args.size(); i++)
|
||||||
|
{
|
||||||
|
if (i > 0)
|
||||||
|
write(", ");
|
||||||
|
generate_expr(*call->args[i]);
|
||||||
|
}
|
||||||
|
write(")");
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
generate_expr(*call->func);
|
generate_expr(*call->func);
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
class BankAccount:
|
class BankAccount:
|
||||||
def __init__(self, owner, balance=0):
|
def __init__(self, owner, balance):
|
||||||
self.owner = owner
|
self.owner = owner
|
||||||
self.balance = balance
|
self.balance = balance
|
||||||
|
|
||||||
|
|
@ -10,7 +10,7 @@ class BankAccount:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def withdraw(self, amount):
|
def withdraw(self, amount):
|
||||||
if 0 < amount <= self.balance:
|
if 0 < amount and amount <= self.balance:
|
||||||
self.balance -= amount
|
self.balance -= amount
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
@ -19,7 +19,9 @@ class BankAccount:
|
||||||
return self.balance
|
return self.balance
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"Account({self.owner}, Balance: ${self.balance:.2f})"
|
owner = self.owner
|
||||||
|
balance = self.balance
|
||||||
|
return f"Account({owner}, Balance: {balance})"
|
||||||
|
|
||||||
account = BankAccount("Alice", 1000)
|
account = BankAccount("Alice", 1000)
|
||||||
print(account)
|
print(account)
|
||||||
|
|
|
||||||
36
test/ruby/28.out
Normal file
36
test/ruby/28.out
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
class BankAccount
|
||||||
|
def initialize(owner, balance)
|
||||||
|
@owner = owner
|
||||||
|
@balance = balance
|
||||||
|
end
|
||||||
|
def deposit(amount)
|
||||||
|
if amount > 0
|
||||||
|
@balance += amount
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
def withdraw(amount)
|
||||||
|
if 0 < amount and amount <= @balance
|
||||||
|
@balance -= amount
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
def get_balance
|
||||||
|
return @balance
|
||||||
|
end
|
||||||
|
def to_s
|
||||||
|
owner = @owner
|
||||||
|
balance = @balance
|
||||||
|
return "Account(#{owner}, Balance: #{balance})"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
account = BankAccount.new("Alice", 1000)
|
||||||
|
print(account, "\n")
|
||||||
|
print("Deposit $500: #{account.deposit(500)}", "\n")
|
||||||
|
print("Balance: $#{account.get_balance()}", "\n")
|
||||||
|
print("Withdraw $200: #{account.withdraw(200)}", "\n")
|
||||||
|
print("Balance: $#{account.get_balance()}", "\n")
|
||||||
|
print("Withdraw $2000: #{account.withdraw(2000)}", "\n")
|
||||||
|
print("Final balance: $#{account.get_balance()}", "\n")
|
||||||
34
test/ruby/34.in
Normal file
34
test/ruby/34.in
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
class BankAccount:
|
||||||
|
def __init__(self, owner, balance=0):
|
||||||
|
self.owner = owner
|
||||||
|
self.balance = balance
|
||||||
|
|
||||||
|
def deposit(self, amount):
|
||||||
|
if amount > 0:
|
||||||
|
self.balance += amount
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def withdraw(self, amount):
|
||||||
|
if 0 < amount <= self.balance:
|
||||||
|
self.balance -= amount
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def get_balance(self):
|
||||||
|
return self.balance
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f"Account({self.owner}, Balance: ${self.balance:.2f})"
|
||||||
|
|
||||||
|
account = BankAccount("Alice", 1000)
|
||||||
|
print(account)
|
||||||
|
|
||||||
|
print(f"Deposit $500: {account.deposit(500)}")
|
||||||
|
print(f"Balance: ${account.get_balance()}")
|
||||||
|
|
||||||
|
print(f"Withdraw $200: {account.withdraw(200)}")
|
||||||
|
print(f"Balance: ${account.get_balance()}")
|
||||||
|
|
||||||
|
print(f"Withdraw $2000: {account.withdraw(2000)}")
|
||||||
|
print(f"Final balance: ${account.get_balance()}")
|
||||||
Loading…
Add table
Add a link
Reference in a new issue