How to do comparison and adding AtomicInteger - concurrency

There is such synchronized function:
private int balance;
//...
public synchronized void executeRequest(Request request) {
switch (request.getType()) {
case CREDIT:
if(balance >= request.getAmount()) {
balance -= request.getAmount();
System.out.printf(SUCCESS,
request,
Thread.currentThread().getName(),
balance);
} else {
System.out.printf(FAILURE,
request,
Thread.currentThread().getName(),
balance);
}
break;
case REPAYMENT:
balance += request.getAmount();
System.out.printf(SUCCESS,
request,
Thread.currentThread().getName(),
balance);
break;
}
}
I need to rewrite it by using AtomicInteger for balance parameter.
I can't come up with how to do two actions atomically:
balance >= request.getAmount()
balance -= request.getAmount();
I try to rewrite CREDIT operation like that:
private void credit(int amount, String request) {
int oldValue;
int newValue;
do {
oldValue = balance.get();
newValue = oldValue - amount;
if (oldValue >= amount) {
System.out.printf(SUCCESS,
request,
Thread.currentThread().getName(),
balance);
} else {
System.out.printf(FAILURE,
request,
Thread.currentThread().getName(),
balance);
}
} while (!balance.compareAndSet(oldValue, newValue));
}
But it won't work, because there's no guarantee that oldValue >= amount will be true when we try to compute !balance.compareAndSet(oldValue, newValue) again.
Have you any idea how to rewrite the first method with AtomicInteger?

I came up with a such decision. What do you think about it?
private void credit(int amount, String request) {
int oldValue;
int newValue;
while (true) {
oldValue = balance.get();
newValue = oldValue - amount;
if (oldValue >= amount && balance.compareAndSet(oldValue, newValue)) {
System.out.printf(SUCCESS,
request,
Thread.currentThread().getName(),
newValue);
break;
} else if (oldValue < amount) {
System.out.printf(FAILURE,
request,
Thread.currentThread().getName(),
oldValue);
break;
}
}
}

Related

Error: no matching function for call to ‘Order::Order()’

#include <iostream>
using namespace std;
class Customer
{
public :
string email, name, address;
long telephoneNo;
int addNew()
{
cout<<"Enter the email, name, address and telephone no. of the user Please.\n";
cin>>email>>name>>address>>telephoneNo;
return 0;
}
int edit()
{
cout<<"Enter new name, email, address and telephone no. of user Please.\n";
cin>>email>>name>>address>>telephoneNo;
return 0;
}
int update()
{
cout<<"Enter new email, address and telephone no. of user Mr./Ms. "<<name<<" Please.\n";
cin>>email>>address>>telephoneNo;
return 0;
}
};
class Order : public Customer
{
public :
static int number;
const float price = 10000.0;
float payment;
string state, billingAddress;
bool created, paymentMade;
Customer *customer;
Order(Customer *customer)
{
number++;
this->customer = customer;
payment = price + (0.1f*price);
state = "West Bengal";
billingAddress = customer->address;
created = true;
paymentMade = false;
}
int setState(string state)
{
this->state = state;
return 0;
}
float getPrice()
{
return price;
}
};
int Order::number = 0;
class Passenger : public Order
{
public :
string name;
bool insurance, priorityBoarding;
float luggage, extraLuggage;
Order *order;
Passenger(Order *order)
{
char flag;
this->order = order;
name = order->customer->name;
cout<<"Enter the luggage amount for the customer Mr./Ms. "<<name<<", in kgs Please.\n";
cin>>luggage;
if(luggage > 15.0)
extraLuggage = luggage - 15.0f;
cout<<"Is the customer eligible for insurance. y/n? \n";
cin>>flag;
if(flag == 'y')
insurance = true;
else
insurance = false;
cout<<"Is the customer a priority boarder. y/n? \n";
cin>>flag;
if(flag == 'y')
priorityBoarding = true;
else
priorityBoarding = false;
}
Passenger get()
{
return *this;
}
int edit()
{
char flag;
cout<<"Please re-enter the name of the Passenger.\n";
cin>>name;
cout<<"Enter the luggage amount for the customer Mr./Ms. "<<name<<", in kgs Please.\n";
cin>>luggage;
if(luggage > 15.0)
extraLuggage = luggage - 15.0f;
cout<<"Is the customer eligible for insurance. y/n? \n";
cin>>flag;
if(flag == 'y')
insurance = true;
else
insurance = false;
cout<<"Is the customer a priority boarder. y/n? \n";
cin>>flag;
if(flag == 'y')
priorityBoarding = true;
else
priorityBoarding = false;
return 0;
}
int update()
{
char flag;
cout<<"Enter the luggage amount for the customer Mr./Ms. "<<name<<", in kgs Please.\n";
cin>>luggage;
if(luggage > 15.0)
extraLuggage = luggage - 15.0f;
cout<<"Is the customer eligible for insurance. y/n? \n";
cin>>flag;
if(flag == 'y')
insurance = true;
else
insurance = false;
cout<<"Is the customer a priority boarder. y/n? \n";
cin>>flag;
if(flag == 'y')
priorityBoarding = true;
else
priorityBoarding = false;
return 0;
}
};
class OrderFlight : public Order, public Passenger
{
public :
static int flights;
Passenger* passenger;
int addPassenger(Order *order)
{
Passenger* passenger = new Passenger(&order);
this->passenger = passenger;
return 0;
}
int removePassanger()
{
delete(&passenger);
return 0;
}
/*
float getPrice(Passenger *passenger)
{
Passenger* gPassenger = Passenger::get();
}
*/
};
int OrderFlight::flights = 50;
int main()
{
cout<<"Hello World";
return 0;
}
Getting the error ---
In constructor ‘Passenger::Passenger(Order*)’:
main.cpp:84:5: error: no matching function for call to ‘Order::Order()’
I am trying to send a pointer of the object of class Order in the constructor of Passenger, but getting this error instead. Did the same thing while passing the object or Order in the addPassenger() method of the OrderFlight class but did not get any error. Please help.
class Passenger : public Order says that a Passenger is an Order. That doesn't really make sense, and ultimately is the source of your error:
Part of constructing a Passenger involves constructing all of its base classes, which includes Order. However, Order does not have a default constructor, and you do not use a delegating constructor to tell it how to construct the Order part of itself, so it doesn't know what to do.
I would rethink your inheritance hierarchy here. Like what I mentioned above, class Order : public Customer doesn't make sense because it implies that Orders are Customers. class OrderFlight : public Order, public Passenger also doesn't make sense for the same reason.

How to initialize an object once and use it for all test cases

I have a test class where I have all my test cases for my java project, I want to initialize the service class once which creates an array and I want to use the same array for all the other test cases, I have tried to do that but when the first test case is ran an customer is registered and stored in the array I want to use the customer stored in the array and use it for the next test case but it seems that the customer is not in the array for the next test case i.e a new array is being created or I think that junit is running each test case individually
MainTests.java
package tests;
import static org.junit.Assert.*;
import org.junit.*;
import services.BankingServiceImpl;
import dao.BankingSystemArrayImpl;
public class MainTests {
static BankingServiceImpl bankingService;
private static boolean setupIsDone = false;
#BeforeClass
public static void setup() {
bankingService = new BankingServiceImpl();
}
#Test // 1
public void createCustomer() {
String custName = "Rohan";
String HomeAddressCity = "Pune";
String HomeAddressState = "Maharashtra";
int HomeAddressPincode = 411043;
String LocalAddressCity = "Pune";
String LocalAddressState = "Pune";
int LocalAddressPincode = 411043;
int day = 19;
int month = 9;
int year = 1998;
int result = bankingService.acceptCustomerDetails(custName, HomeAddressCity, HomeAddressState,
HomeAddressPincode, LocalAddressCity, LocalAddressState, LocalAddressPincode, day, month, year);
System.out.println(bankingService.getLength());
assertTrue(result > 0);
}
#Test // 2
public void openCustomerAccount() {
System.out.print(bankingService.getLength());
int customerid = 100;
int balance = 100000;
String accountType = "savings";
int result = bankingService.openAccount(customerid, balance, accountType);
assertTrue(result > 0);
}
#Test // 3
public void Balance() {
int customerid = 100;
int accountid = 50;
int pin = 1007;
int result = bankingService.getAccountBalance(customerid, accountid, pin);
assertTrue(result > 0);
}
#Test // 4
public void amt_withdraw() {
int customerid = 100;
int accountid = 50;
int amount = 10000;
int pin = 1007;
int result = bankingService.withdraw(customerid, accountid, amount, pin);
assertEquals(result, 90000);
}
#Test // 5
public void transfer() {
int customerid = 100;
int accountid = 50;
int customerid1 = 101;
int accountid1 = 51;
int amount = 10000;
int pin = 1007;
boolean result = bankingService.fundTransfer(customerid, accountid, customerid1, accountid1, amount, pin);
assertEquals(result, true);
}
#Test // 6
public void amt_deposit() {
int customerid = 100;
int accountid = 50;
int amount = 10000;
int result = bankingService.deposit(customerid, accountid, amount);
assertEquals(result, 90000);
}
/*
* #Test //7 public void cust_details() { int customerid = 100;
* BankingServiceImpl bankingService = new BankingServiceImpl();
*
* int result = bankingService.getCustomerDetails(customerid);
* assertEquals(result, 90000); }
*/
#Test // 10
public void pin_change() {
int customerid = 100;
int accountid = 50;
int o_pin = 1007;
int n_pin = 1122;
boolean result = bankingService.changePin(customerid, accountid, o_pin, n_pin);
assertEquals(result, true);
}
#Test // 11
public void check_change() {
int customerid = 100;
int accountid = 50;
int pin = 1007;
boolean result = bankingService.checkPin(customerid, accountid, pin);
assertEquals(result, true);
}
}
Service Class
package services;
import beans.Account;
import beans.Address;
import beans.Customer;
import beans.MyDate;
import beans.Transaction;
import dao.BankingSystemArrayImpl;
public class BankingServiceImpl {
BankingSystemArrayImpl BankingSystemArray;
public BankingServiceImpl() {
System.out.print("called");
BankingSystemArray = new BankingSystemArrayImpl();
}
/*
* public void transfer(int accountId, int tansferAccountId, double amount)
* { double a = BankingSystemArray.getAccount(accountId).getBalance()
* - amount; System.out.println(a);
* BankingSystemArray.getAccount(accountId).setBalance(a); double b =
* BankingSystemArray.getAccount(accountId).getBalance() + amount;
* BankingSystemArray.getAccount(tansferAccountId).setBalance(b);
*
* }
*/
public int acceptCustomerDetails(String custName, String HomeAddressCity,
String HomeAddressState, int HomeAddressPincode,
String LocalAddressCity, String LocalAddressState,
int LocalAddressPincode, int day, int month, int year) {
if ((day > 0 && day <= 31) && (month >= 1 && month <= 12)
&& (year <= 2015)) {
return BankingSystemArray.insertCustomer(new Customer(
custName, new Address(LocalAddressCity, LocalAddressState,
LocalAddressPincode), new Address(HomeAddressCity,
HomeAddressState, HomeAddressPincode), new MyDate(
day, month, year)));
} else
return 0;
}
public int openAccount(int custId, int balance, String accType) {
int accountId = 0;
if (custId < 99) {
System.out
.println("Invalid customer Id,please enter a valid customer Id");
} else if (!(accType.equalsIgnoreCase("savings")
|| accType.equalsIgnoreCase("current") || accType
.equalsIgnoreCase("salary"))) {
System.out
.println("Invalid account type, please enter a valid account type");
} else if (balance < 0) {
System.out.println("Invalid amount, please amount a valid amount");
}
else {
Customer customer = BankingSystemArray.getCustomer(custId);
if (customer == null) {
System.out.println("Sorry you have not registered");
return 0;
} else {
Account account = new Account(accType, balance);
accountId = BankingSystemArray.insertAccount(account,
custId);
}
}
return accountId;
}
public int getAccountBalance(int custId, int accNo, int pin) {
if (checkPin(custId, accNo, pin)) {
return BankingSystemArray.getAccount(custId, accNo)
.getBalance();
} else {
System.out.println("Invalid pin");
return 0;
}
}
public int withdraw(int custId, int accNo, int amt, int pin) {
int balance = 0;
if (amt < 0) {
System.out.println("Invalid amount, please enter a valid amount");
} else {
Customer customer = BankingSystemArray.getCustomer(custId);
if (customer == null) {
return 0;
} else {
Account account = BankingSystemArray.getAccount(custId,
accNo);
if (account == null) {
System.out.println("Sorry your account does not exist");
} else if (account.getPin()!=pin) {
System.out.println("Invalid pin");
return 0;
} else {
if ((account.getBalance() - amt) > 0) {
account.setBalance(account.getBalance() - amt);
balance = account.getBalance();
}
}
}
}
return balance;
}
public boolean fundTransfer(int custIdFrom, int accNoFrom, int custIdTo,
int accNoTo, int amt, int pin) {
if (withdraw(custIdFrom, accNoFrom, amt, pin) > 0) {
deposit(custIdTo, accNoTo, amt);
return true;
}
return false;
}
public int deposit(int custId, int accNo, int amt) {
if (amt < 0) {
System.out.println("Invalid amount, please enter a valid amount");
} else {
BankingSystemArray.getAccount(custId, accNo).setBalance(
BankingSystemArray.getAccount(custId, accNo)
.getBalance() + amt);
return BankingSystemArray.getAccount(custId, accNo)
.getBalance();
}
return 0;
}
public Customer getCustomerDetails(int custId) {
Customer customer = BankingSystemArray.getCustomer(custId);
if (customer != null) {
return customer;
}
return null;
}
public Account getAccountDetails(int custId, int accNo) {
Account account = BankingSystemArray.getAccount(custId, accNo);
if (account != null) {
return account;
}
return null;
}
public Account[] getAllAccountsDetails(int custId) {
Account[] account = BankingSystemArray.getAccountList(custId);
if (account != null) {
return account;
}
return null;
}
public Transaction[] getAllTransactionDetails(int custId, int accNo) {
// TODO Auto-generated method stub
return null;
}
public int generatePin(int custId, int accNo) {
Account account = BankingSystemArray.getAccount(custId, accNo);
int pin = BankingSystemArray.generateRandomNumber();
account.setPin(pin);
return account.getPin();
}
public boolean changePin(int custId, int accNo, int oldPin, int newPin) {
Account account = BankingSystemArray.getAccount(custId, accNo);
if (account != null) {
if (account.getPin() == oldPin) {
account.setPin(newPin);
return true;
}
}
return false;
}
public boolean checkPin(int custId, int accNo, int pin) {
Account account = BankingSystemArray.getAccount(custId, accNo);
if (account != null) {
if (account.getPin() == pin) {
return true;
}
}
return false;
}
public Customer getLength() {
return BankingSystemArray.getLength();
}
}
To solve your problem, you need first to understand the JUnit behavior.
Methods marked with #BeforeClass and #AfterClass are run only
once, and are the upper and lower bound of all your test methods.
Methods marked with #Before and #After are run immediately
before and after any test method.
Only static members are shared between test cases. So all instance
members are being reset before any test method.
Also note that as a RULE in Unit Testing, all tests (all test methods) MUST be isolated from each other, but it's OK in integration testing.

char* element of class is empty

I am relatively new to C++ and classes. When I pass a char* through to the class constructor and copy it into the m_make and m_model char*'s, they are still empty. They are not pointing to random bits of memory when I print them out, no data is outputted, just blank space.
Constructor:
RentalCar::RentalCar(int year, char * make, char * model, float price, bool available)
{
m_year = year;
myStringCopy(m_make, make);
myStringCopy(m_model, model);
m_price = price;
m_available = available;
}
EDIT:
void readFile(RentalAgencyADT * list, ifstream & file)
{
char letter;
char word[7];
int i = 0; //position in word
int j = 0; //position in RentalCar data
int k = 0; //position of line in RentalAgencyADT
int l = 0; //position of RentalAgencyADT
while((letter = file.get()) != EOF)
{
if(letter == ' ' || letter == '\n')
{
int year;
char make[7], model[7];
float price;
bool available;
i = 0;
if(k != 0)
{
switch(j)
{
case 0:
year = atoi(word);
j++;
break;
case 1:
myStringCopy(make,word);
j++;
break;
case 2:
myStringCopy(model,word);
j++;
break;
case 3:
price = atof(word);
j++;
break;
case 4:
available = (bool) atoi(word);
list[l].inventory[k - 1] = RentalCar(year,make,model,price,available);
j = 0;
k++;
break;
}
clearWord(word);
i = 0;
if(k == 7)
{
}
}
else if(k == 0)
{
switch(j)
{
case 0:
myStringCopy(list[l].name, word);
j++;
break;
case 1:
//intCopy((list[l].zipcode),word);
//cout << list[l].zipcode << endl;
for(int i = 0; i < 5; i++)
{
list[l].zipcode[i] = word[i] - '0';
cout << list[l].zipcode[i];
}
j = 0;
k++;
break;
}
clearWord(word);
}
if(j == 4)
{
clearWord(make);
clearWord(model);
}
}
else
{
word[i] = letter;
i++;
}
}
}
Here is the RentalCar.cpp file:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "RentalCar.h"
using namespace std;
char* myStringCopy(char * destination, char * source);
RentalCar::RentalCar(int year, char * make, char * model, float price, bool available)
{
m_year = year;
myStringCopy(m_make, make);
myStringCopy(m_model, model);
m_price = price;
m_available = available;
}
char* myStringCopy(char * destination, char * source)
{
int i = 0;
while(source[i] != '\0')
{
destination[i] = source[i];
i++;
}
destination[i] = '\0';
return destination;
}
int RentalCar:: getYear()
{
return m_year;
}
void RentalCar:: setYear(int year)
{
m_year = year;
}
char* RentalCar:: getMake()
{
return m_make;
}
void RentalCar:: setMake(char * make)
{
myStringCopy(m_make, make);
}
char* RentalCar:: getModel()
{
return m_model;
}
void RentalCar:: setMode(char * model)
{
myStringCopy(m_model, model);
}
float RentalCar:: getPrice()
{
return m_price;
}
void RentalCar:: setPrice(int price)
{
m_price = price;
}
bool RentalCar:: getAvailable()
{
return m_available;
}
void RentalCar::setAvailable(bool available)
{
m_available = available;
}
void RentalCar::print()
{
cout << m_year << " " << m_make << " " << m_model << ", $" << m_price << ", Available: " << boolalpha << m_available << endl;
}
float RentalCar::estimateCost(int days)
{
return m_price * days;
}
I was using the strcpy() function earlier just to test but it didnt work either.
Headerfile:
#ifndef RENTALCAR_H_
#define RENTALCAR_H_
class RentalCar
{
public:
RentalCar(const int year, char * make, char * model, const float price, bool available);
int getYear();
void setYear(int year);
char* getMake();
void setMake(char make[]);
char* getModel();
void setMode(char model[]);
float getPrice();
void setPrice(int price);
bool getAvailable();
void setAvailable(bool available);
void print();
float estimateCost(int days);
protected:
private:
int m_year;
char * m_make;
char * m_model;
float m_price;
bool m_available;
};
#endif
You are strcpy()-ing into un-initialized char * pointers, which is undefined behaviour.
At the least you should be using strdup(), e.g. m_make = strdup(make);, with a corresponding free() in the destructor.

C++ program on single serving queing

I am a noobie with C++, and have only basic knowledge. I am trying to make a program for my 'operation research' class. I have written a code that works like a flowchart for a single server queuing system. I am getting multiple errors (which seem simple) but cant figure out how to fix them (being a noobie). Some examples are 'missing terminating character', 'stray', and 'not declared in this scope'. All help is much appreciated.
#include <iostream>
#include <cmath>
using namespace std;
const int BUSY=1;
const int IDLE=0;
int
choice,
WL,
SS;
double
MX,
IT,
ST,
TM,
AT,
DT,
TypeOfEvent,
NextServiceTime,
ProgressAT’
ProgressDT;
void initialize();
void Timing();
void Arrival();
void Departure();
float expon(float mean);
int main()
{
initialize();
do
{
cout<<"Enter your Policy: ";
cin>>choice;
}
while(choice>2||choice<1);
cout<<"'End of Simulation’ Time: "<<MX<<endll;
while(true)
{ Timing(); //To Determine The Next Event
if(TM>MX)
break;
switch (int(TypeOfEvent))
{ case 1:
Arrival();
break;
case 2:
Departure();
break;
}
}
return 0;
}
void initialize()
{ IT=1.0;
ST=0.5;
TM = 0.0;
SS = IDLE;
WL= 0;
AT= TM + expon(IT); //Arriving
NextServiceTime = expon(ST);
DT= 1.0e+10; // Guaranteeing that the first event is arriving
ProgressAT=0.0;
ProgressDT = 0.0;
}
void Timing()
{
TypeOfEvent = 0;
if(AT < DT)
{
TypeOfEvent = 1;
TM=AT;
}
else
{
TypeOfEvent = 2;
TM= DT;
}
if (TypeOfEvent == 0)
{
cout<<"System Idle "<<TM;
exit(1);
}
}
void Arrival()
{
if (SS == BUSY)
{
++WL;
ServiceTime[WL] = NextServiceTime;
}
else
{
SS = IDLE;
DT = TM + NextServiceTime;
}
AT = TM + expon(IT);
NextServiceTime = expon(ST);
}
void Departure()
{
if (WL == 0)
{
SS= IDLE;
DT = 9999;
}
else
{
if(choice==2)
{
DT= TM + NextServiceTime;
}
--WL;
}
}

Make tree from mathematical expression by std::regex

Programm get string like:VARIABLE=EXPRESSION.
where VARIABLE is some letters and numbers, starting letter
and EXPRESSION is mathematical expression that can include:
1) + or *
2) ( or )
3) numbers (for example, 5; 3.8; 1e+18, 8.41E-10)
4) another names of variables.
I need to turn this into the tree (just in memory) using one regular exprassion.
How can i do this?
Was proposed method:
search for = out of ()
search for + out of ()
search for * out of ()
if somethig found - break expression in 2 parts, else we got number or variable.
Looks pretty simple, but i can't create this expressions.
I'm alse found how to put conditional operator into regular expression.
Mostly, i just need 3 regular exprassions for =, + and *.
Example:
http://i.stack.imgur.com/I8R12.png
As stated by n.m., regular expressions can't deal with nested parentheses. However there are simple alternatives that can parse nested parenthesis, such as recursive descent parsers.
Example:
enum TokenType
{
TTId,
TTNumber,
TTPlus,
TTMinus,
TTTimes,
TTDivide,
TTLParen,
TTRParen,
TTEndOfInput
};
TokenType token = TTEndOfInput;
string tokenValue;
int peekChar();
void nextChar();
void error(string msg); // doesn't return
Value *createConstant(string value);
Value *createReadVariable(string name);
Value *createAdd(Value *l, Value *r);
Value *createSubtract(Value *l, Value *r);
Value *createMultiply(Value *l, Value *r);
Value *createDivide(Value *l, Value *r);
Value *createNegate(Value *l, Value *r);
Value *expression();
void getToken()
{
token = TTEndOfInput;
tokenValue = "";
if(peekChar() == EOF)
return;
if(isalpha(peekChar()))
{
while(isalnum(peekChar()))
{
tokenValue += (char)peekChar();
nextChar();
}
token = TTId;
return;
}
if(isdigit(peekChar()) || peekChar() == '.')
{
while(isdigit(peekChar()))
{
tokenValue += (char)peekChar();
nextChar();
}
if(peekChar() == '.')
{
tokenValue += (char)peekChar();
nextChar();
while(isdigit(peekChar()))
{
tokenValue += (char)peekChar();
nextChar();
}
if(tokenValue == ".")
error("missing digit");
}
if(peekChar() == 'e')
{
tokenValue += (char)peekChar();
nextChar();
if(peekChar() == '+' || peekChar() == '-')
{
tokenValue += (char)peekChar();
nextChar();
}
if(!isdigit(peekChar()))
error("missing digit");
while(isdigit(peekChar()))
{
tokenValue += (char)peekChar();
nextChar();
}
}
token = TTNumber;
return;
}
switch(peekChar())
{
case '+':
token = TTPlus;
nextChar();
return;
case '-':
token = TTMinus;
nextChar();
return;
case '*':
token = TTTimes;
nextChar();
return;
case '/':
token = TTDivide;
nextChar();
return;
case '(':
token = TTLParen;
nextChar();
return;
case ')':
token = TTRParen;
nextChar();
return;
default:
error("invalid charater");
}
}
Value *topLevel()
{
Value *retval;
switch(token)
{
case TTId:
retval = createReadVariable(tokenValue);
getToken();
return retval;
case TTNumber:
retval = createConstant(tokenValue);
getToken();
return retval;
case TTLParen:
getToken();
retval = expression();
if(token != TTRParen)
error("expected )");
getToken();
return retval;
case TTMinus:
getToken();
return createNegate(topLevel());
default:
error("unexpected token");
}
}
Value *mulDiv()
{
Value *retval = topLevel();
while(token == TTTimes || token == TTDivide)
{
TokenType operation = token;
getToken();
Value *rhs = topLevel();
if(operation == TTTimes)
{
retval = createMultiply(retval, rhs);
}
else // operation == TTDivide
{
retval = createDivide(retval, rhs);
}
}
return retval;
}
Value *addSub()
{
Value *retval = mulDiv();
while(token == TTPlus || token == TTMinus)
{
TokenType operation = token;
getToken();
Value *rhs = mulDiv();
if(operation == TTPlus)
{
retval = createAdd(retval, rhs);
}
else // operation == TTMinus
{
retval = createSubtract(retval, rhs);
}
}
return retval;
}
Value *expression()
{
return addSub();
}
void error(string msg)
{
cerr << "error : " << msg << endl;
exit(1);
}
int main()
{
getToken();
Value *expressionTree = expression();
// ...
return 0;
}